jQuery/Ajax "Email Is Available" Check

I found a registration script that queries a database to see if a username is available. I want to change it to see if an email is available. I was able to change it to check for email from the database, however, I need to add a function or a jquery call to verify that the email has proper syntax (require “@” and “.”). The issue is that its checking the email from the database, but even if you type a non valid email like “adfasdfasdf” it will come back with a checkmark, even though its not a proper email format. So this is what Im trying to fix…

Here is the example of the username script
http://papermashup.com/demos/check-username

And here is my version of the same script, but I changed all the variables to be email instead of username (and it works). And I think I found the right spot to put the email validation, but not too sure. See “&& isValid(email)” halfway through the script below:

function email_check(){	
	
	var email = $('#my_email').val();
	
	if(email == "" || email.length < 8){

		$('#my_email').css('border', '1px #CCC solid');
		$('#tick').hide();
		
	} else {
	
		jQuery.ajax({
		   type: "POST",
		   url: "ajax-email-validate.php",
		   data: 'email='+ email,
		   cache: false,
		   success: function(response){
			if(response == 1 && isValid(email) ){
					$('#my_email').css('border', '1px #C33 solid');	
					$('#tick').hide();
					$('#cross').fadeIn();
				} else {
					$('#my_email').css('border', '1px #090 solid');
					$('#cross').hide();
					$('#tick').fadeIn();
				}
			
			}
		});
	}
}

Is “&& isValid(email)” the correct spot to check if the email is valid? Should I write a javascript function to check for @ and . or is there a jQuery way? I know there has to be…

thanks for any suggestions!

It’s not a huge deal, but if the email is not syntactically valid, I wouldn’t even bother asking the server if it’s available.

right and the first step is to determine using javascript if the email is valid, which is what I am trying to do. :slight_smile: Any ideas?

Oh, sorry.

Some good info is here. It raises some important points about how thorough you should validate(more may not be better).
http://www.regular-expressions.info/email.html

This seems decent to me.


function validEmail(email) {
    return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$/i.test(email);
}