SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    email validation

    I have searched through the forums but can't find quite what I need. I have a form that needs to verify the email address with specific requirements... the email field should have only one '@' in it and may contain letters, numbers, and the following characters: hyphen, underscore and period.
    Also, the first character must be a letter. If this field fails these validations, it will display an alert box informing the user of the specific error and place the focus on the Email Id field with the text selected.

    I have figured out how to make sure that the field isn't empty, but can't figure out the rest... any suggestion or help would be greatly appreciated

    Code:
    function validEmail()
    {
         if (document.myForm.emailId.value == "")
        {
    		alert("Please enter a valid email")
    		return false;
        }	
    return true;
    }

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,439
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    regex

    It does help to know what words to use when searching. The word you want to use here is "regex". Search for - email address regex - and you'll get enough results to make your head spin.

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, thanks... there is a ton of info on regex... a little overwhelming for a newbie, but I've been reading up on it all day and I think I get it a little better now... but my script still isn't working right...
    Code:
    // Email Requirements function
    var at = ("\@");
    var firstLetter = /^([a-zA-Z_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailId = ("document.myForm.emailId.value");
    
    function validEmail()
    {
    	with (emailId)
    	{
    		if (document.myForm.emailId.value == "")
        	{
    		alert("Please enter a valid email")
    		myForm.emailId.focus();
    		return false;
    		}
    		if (at < 1 || at > 1)
    		{
    			alert("You have the wrong amount of @ symbols, Please enter a valid email");
    			myForm.emailId.focus();
    			return false;
    		}
    		if (firstLetter.test())
    		{
    		return addArray();
    		}
    		else
    		{
    			alert("Your email must begin with a letter, Please enter a valid email");
    			myForm.emailId.focus();
    			return false;
    		}
    	}
    	return addArray();
    }
    I'm not getting any browser errors, but the areas that will not work are while checking for the "@" and that the first character must be a letter. It completely ignores if you use more than one "@" and it will alert to enter a valid email whether or not the first character is a letter.

    Also, I need to have the email field selected after the alert appears when there is an error. I tried myForm.emailId.focus.select(); but it gave me errors.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,439
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    email regex

    I agree that regex can be a bit overwhelming for a newbie. Believe me, it can be a bit overwhelming for the not-so-newbie too! What with all the different "flavors" that are similar, but different, all the various ways to get the same result, and how other times one little change makes a world of difference, even developers that are gurus in other areas can have trouble with regex. Especially confusing to me are the "short-cuts" eg.
    /[a-zA-Z]/ is equivalent to /[a-z]/i
    /[a-zA-Z_]/ is equivalent to /[\w]/
    Anyway, once you get a good regex, testing for empty '' and multiple ats "@" won't be neccessary, the regex will take care of those. Then you'll only need
    Code:
    // if email OK
    {
    //do stuff
    }
    else
    {
    // alert and return focus
    }
    The regex you have looks good (with the possible exception that because the "@" isn't inside a character class, you don't need/shouldn't have the slash). It could be refined a little bit, but for now it should be good enough so you can iron out the rest of the script.
    I don't know, but the focus error might be able to be fixed by using something like
    Code:
    var email_input = document.getElementById('emailId');
    email_input.focus();

  5. #5
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help! I think I'm going to stay away from the shortcuts, at least until I understand the full codes anyway
    and I tried the focus code and it works! thank you so much!

  6. #6
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there any good guidelines for email validation? Whenever I try to search, I just get an avalanche of peoples' own custom-made functions or tutorials on regexes. Regex I can do fine, but is there any definitive standard for what an email address can or cannot look like?

    Cheers,
    D.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,439
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    standard

    There's standards, and there's email client support. AFAIK the current standard is RFC 2822. For the technical read -> http://tools.ietf.org/html/rfc2822
    the wiki says
    this_part_can_have_up_to_64_chars@and_this_part_up_to_255_chars
    but I have never seen one that long.
    The part after the @ should be a domain, so I guess what ever rules apply for valid domains also apply to the email address. ie. alphanums and "-" and "."
    The part before the @, the "local" part, as per the wiki
    • Uppercase and lowercase letters (case sensitive)
    • The digits 0 through 9
    • The characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
    • The character . provided that it is not the first or last character in the local-part.
    But hotmail only allows alphanums and "_" , "-" , and "."

  8. #8
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Hotmail convention seems considerably saner than allowing nearly everything in the printable ASCII range. I can't honestly say that in all my years on the internet that I've ever seen an email address consisting of any of those characters (! # $ &#37; & ' * + - / = ? ^ _ ` { | } ~), except the dash and underscore.

    Cheers,
    D.


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
  •