Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    email validation needs tweaking

    I'm using the following code for an email validator:

    Code:
    function emailCheck(emailStr) {
    
    // checks if the e-mail address is valid
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Your email destination IP address is invalid!");
    return false;
          }
       }
    }
    return true;
    }
    But the following email address get rejected:

    firstname.lastname@gmail.com
    macinaw-brownies@yahoo.com
    momsclub90210@yahoo.com



    and I can't figure out why. How do I tweak my code so that these email address will be seen as valid?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    var emailPat = /^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([-]+)?([a-z0-9]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i


  •  

    Posting Permissions

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