SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    is this email validation funcation correct?

    Hai folks,

    Code:
    function validateEmail($email){
        $pattern = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$";
        if (preg_match($pattern, $email)==0) {
           echo "Email id not valid.";
           return false;
        }
    }

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    There are actually a bunch of symbols which are valid in email address that you didn't account for:
    ! # $ % & ' * + - / = ? ^ _ ` { | } ~

    Also, with .'s, they can't be the first or last character and there can't be more than one in a row (so no bob..smith@email.com).

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samanime View Post
    There are actually a bunch of symbols which are valid in email address that you didn't account for:
    ! # $ % & ' * + - / = ? ^ _ ` { | } ~

    Also, with .'s, they can't be the first or last character and there can't be more than one in a row (so no bob..smith@email.com).
    Thanks for the feedback, let me find a more suitable one then in google

  4. #4
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is a script that works well. The email address in this case is in a form called ProductOrder.

    Code JavaScript:
    // check email address is valid 
              var checkEmail=document.ProductOrder.emailAddress.value;
          // check if entry
          // removes spaces if any
              checkEmail=checkEmail.replace(/ /gi,"")                              
              document.ProductOrder.emailAddress.value=checkEmail;                       // return corrected value to field 
         // check format of email address
              var ok="-a-z0-9!#$%&'*/=?+^_'{|}~";
              var re=new RegExp("^(\"?)(?:["+ok+"][.]?)*(?:["+ok+"])+\\1@(?:(?:[-a-z0-9])+[.])+(?:[a-z]{2}|[a-z]{3}(?:[.][a-z]{2})?)$","i"); 
         //
              var resultObj=re.exec(checkEmail);
              if( !resultObj || !resultObj[0])              // no match found  
                   { alert("Please enter a valid email address before re-submitting")
                      return false                          
                   }
     // -----


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
  •