0

I have used following code to ensure email address provided on signup are valid.

if (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
    $msg = $msg."Email Id Not Valid, Please Enter The Correct Email Id .<BR>";
    $status = "NOTOK";
}

I entered a standard email myemail@gmail.com but it's flagging it as invalid.

Please help me why it is flagging it.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Manisha
  • 57
  • 1
  • 9
  • 1
    Why do you put `!` at the beginning ? – nice_dev Aug 18 '18 at 05:32
  • Please, have a look at these sites: TLD list: https://www.iana.org/domains/root/db ; valid/invalid addresses: https://en.wikipedia.org/wiki/Email_address#Examples ; regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto Aug 18 '18 at 07:54

3 Answers3

1

you should not be using a regular expression for this when there is built in validation:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $msg.="Email Id Not Valid, Please Enter The Correct Email Id ."; 
    $status= "NOTOK";
}

http://php.net/manual/en/filter.examples.validation.php

0

Try this

if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i", $email)) 
{  
   $emailErr = "Email Id Not Valid, Please Enter The Correct Email Id";
}
Ankita Kuchhadiya
  • 1,255
  • 6
  • 16
  • Please, have a look at these sites: TLD list: https://www.iana.org/domains/root/db ; valid/invalid addresses: https://en.wikipedia.org/wiki/Email_address#Examples ; regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto Aug 18 '18 at 07:55
0

Try this...

if(isset($_POST["email"]))
{
        $emaill = filter_var($_POST["email"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);
  // $reg = '/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/';
    if(!preg_match("/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/",$emaill))
        {
            die('<i class="fa fa-times red"></i> Enter valid email!!!');

        }
        else{
             die('<i class="fa fa-check green"></i> Valid!!!');

        }
}
Devyani Kotadiya
  • 450
  • 5
  • 19
  • 1
    there is a lot wrong with this regular expression such as domains that end with more than 4 letters. https://en.wikipedia.org/wiki/Top-level_domain –  Aug 18 '18 at 06:01
  • Please, have a look at these sites: TLD list: https://www.iana.org/domains/root/db ; valid/invalid addresses: https://en.wikipedia.org/wiki/Email_address#Examples ; regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto Aug 18 '18 at 07:55