Jump to content

Regex help for contact form


davelearning

Recommended Posts

Hi all,

 

No matter what username I enter, I am receiving the invalid username message:

$(function() {

// load the modal window
$('a.modal').click(function(){

	// scroll to top
	$('html, body').animate({scrollTop:0}, 'fast');

	// before showing the modal window, reset the form incase of previous use.
	$('.success, .error').hide();
	$('form#contactForm').show();

	// Reset all the default values in the form fields
	$('#name').val('Your name');
	$('#email').val('Your email address');
	$('#comment').val('Enter your comment or query...');

	//show the mask and contact divs
	$('#mask').show().fadeTo('', 0.7);
	$('div#contact').fadeIn();

	// stop the modal link from doing its default action
	return false;
});

// close the modal window is close div or mask div are clicked.
$('div#close, div#mask').click(function() {
	$('div#contact, div#mask').stop().fadeOut('slow');

});

$('#contactForm input').focus(function() {
	$(this).val(' ');
});

$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

// when the Submit button is clicked...
$('input#submit').click(function() {
$('.error').hide().remove();
	//Inputed Strings
	var name = $('#name').val(),
		email = $('#email').val(),
		comment = $('#comment').val();


	//Error Count
	var error_count;

	//Regex Strings
	var name_regex =  /^[a-z0-9_-]{3,15}$/,
		email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;

		//Test Username
		if(!name_regex.test(name)) {
			$('#contact_header').after('<p class=error>Invalid username entered!</p>');
			error_count += 1;
		}

		//Test Email
		if(!email_regex.test(email)) {
			$('#contact_header').after('<p class=error>Invalid email entered!</p>');
			error_count += 1;
		}

		//Blank Comment?
		if(comment == '') {
			$('#contact_header').after('<p class=error>No Comment was entered!</p>');
			error_count += 1;
		}

		//No Errors?
		if(error_count === 0) {
			$.ajax({
				type: "post",
				url: "scripts/send.php",
				data: "name=" + name + "&email=" + email + "&comment=" + comment,
				error: function() {
					$('.error').hide();
					$('#sendError').slideDown('slow');
				},
				success: function () {
					$('.error').hide();
					$('.success').slideDown('slow');
					$('form#contactForm').fadeOut('slow');
				}				
			});	
		}

		else {
                $('.error').show();
            }

	return false;
});

});

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.