SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code JavaScript:
    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!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right and the first step is to determine using javascript if the email is valid, which is what I am trying to do. Any ideas?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    function validEmail(email) {
        return /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i.test(email);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •