SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email validation help

    I got this function from a friend of mine but this has a problem. IF i put special characters like !~%$ etc only in the email field it will tell me that the emial is not valid. If i do some thing like tahirjad*oon@mydomainname.com, this is returned as valid email.

    I want to suppress ~`!#$%^&*()={}[]"':;<>/\|

    this part is not working properly
    Code:
    /* The following string represents the pattern for matching all special
    characters.  We don't want to allow special characters in the address. 
    These characters include ( ) < > @ , ; : \ " . [ ] */
    var specialChars="<A href="file://\\(\\)><@,;:\\\\\\\"\\.\\[\\">\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
     
     
    /* The following string represents the range of characters allowed in a 
    username or domainname. It really states which chars aren't allowed.*/
    var validChars="\[^\\s" + specialChars + "\]";
    Complete function



    Code:
    function emailCheck(strEmailToCheck, blnCheckTLD, blnDetailError) {
    /*
    the function either returns the error number, in this case 0 is no error
    or returns a detailed error string, in this case empty string is no error.
    to get error string you need to pass true as blnDetailError
    whether or not to verify that the address ends in a two-letter country or well-known
    TLD pass, pass blnCheckTLD true means check otherwise pase false means do not check
    number = error string
    **********************************************************************************************
    0 = empty string -- this means ok
    1 = Email address not specified! -- empty string passed for the email address
    2 = Email address seems incorrect (check @ and .'s)
    3 = Email address username contains invalid characters.
    4 = Email address domain name contains invalid characters.
    5 = Email address username doesn't seem to be valid.
    6 = Email address destination IP address is invalid! -- if we have IP for the domain part
    7 = Email address domain name does not seem to be valid.
    8 = Email address must end in a well-known domain or two letter country.
    9 = Email address is missing a hostname!
    */
    if(blnCheckTLD=='undefined' || blnCheckTLD==null || blnCheckTLD=='') blnCheckTLD=false;
    if(blnDetailError=='undefined' || blnDetailError==null || blnDetailError=='') blnDetailError=false;
    if(strEmailToCheck=='undefined' || strEmailToCheck==null) strEmailToCheck='';
    //if email to check is blank, return error immediately
    if(strEmailToCheck == ''){
    if(blnDetailError) return 'Email address not specified!';
    else return 1;
    }
    /* The following is the list of known TLDs that an e-mail address must end with. */
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format. It also is used to separate the username
    from the domain. */
    var emailPat=/^(.+)@(.+)$/;
    /* The following string represents the pattern for matching all special
    characters. We don't want to allow special characters in the address.
    These characters include ( ) < > @ , ; : \ " . [ ] */
    var specialChars="\\(\\)><@,;:\\\\\\\\\.\\[\\];
    /* The following string represents the range of characters allowed in a
    username or domainname. It really states which chars aren't allowed.*/
    var validChars="\[^\\s" + specialChars + "\]";
    /* The following pattern applies if the "user" is a quoted string (in
    which case, there are no rules about which characters are allowed
    and which aren't; anything goes). E.g. "jiminy cricket"@disney.com
    is a legal e-mail address. */
    var quotedUser="(\"[^\"]*\")";
    /* The following pattern applies for domains that are IP addresses,
    rather than symbolic names. E.g. joe@[123.124.233.4] is a legal
    e-mail address. NOTE: The square brackets are required. */
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    /* The following string represents an atom (basically a series of non-special characters.) */
    var atom=validChars + '+';
    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted string. */
    var word="(" + atom + "|" + quotedUser + ")";
    // The following pattern describes the structure of the user
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    /* Finally, let's start trying to figure out if the supplied address is valid. */
    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */
    var matchArray=strEmailToCheck.match(emailPat);
    if (matchArray==null) {
    /* Too many/few @'s or something; basically, this address doesn't
    even fit the general mould of a valid e-mail address. */
    if(blnDetailError) return "Email address seems incorrect (check @ and .'s)";
    else return 2;
    } var user=matchArray[1]; var domain=matchArray[2]; // Start by checking that only basic ASCII characters are in the strings (0-127). for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
    if(blnDetailError) return "Email address username contains invalid characters.";
    else return 3;
    }
    } for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
    if(blnDetailError) return "Email address domain name contains invalid characters.";
    else return 4;
    }
    } // See if "user" is valid if (user.match(userPat)==null) {
    if(blnDetailError) return "Email address username doesn't seem to be valid.";
    else return 5;
    } /* if the e-mail address is at an IP address (not host name) make sure the IP address is valid. */ var IPArray=domain.match(ipDomainPat); if (IPArray!=null) { //We have ourselves an IP address for email address
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    if(blnDetailError) return "Email address destination IP address is invalid!";
    else return 6;
    }
    } //this is valid IP address if(blnDetailError) return ""; else return 0; } // See if "domain" is valid var atomPat=new RegExp("^" + atom + "$"); var domArr=domain.split("."); var len=domArr.length; for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
    if(blnDetailError) return "Email address domain name does not seem to be valid.";
    else return 7;
    }
    } /* domain name seems valid, but now make sure that it ends in a known top-level domain (like com, edu, gov) or a two-letter word, representing country (uk, nl), and that there's a hostname preceding the domain or country. */ if (blnCheckTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
    if(blnDetailError) return "The email address must end in a well-known domain or two letter country.";
    else return 8;
    } // Make sure there's a host name preceding the domain. if (len<2) {
    if(blnDetailError) return "Email address is missing a hostname!";
    else return 9;
    } // If we've gotten this far, everything's valid! if(blnDetailError) return ''; else return 0; }
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  2. #2
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if u want a php solution, i have a nice script. let me know.

  3. #3
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need this javascript. Thanks
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...


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
  •