-2

I have a PHP function to validate e-mail fields. In my PHP file, I receive below error:

Warning: preg_match(): Unknown modifier '_' in C:\xampp\htdocs\validator.inc.php on line 28

My PHP file is:

<?php
define("EMAIL_MASK", "^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");

(...)

function isEmailValid($email)
{
    return !empty($email) && preg_match(EMAIL_MASK, $email); <---- This is the line raising the error
}

(...)

?>

What am I doing wrong?

Willy
  • 9,848
  • 22
  • 141
  • 284

1 Answers1

1

You failed to put the PHP regex delimiters.

define("EMAIL_MASK", "~^[-!#$%&'*+/0-9=?A-Z^_a-z{|}\~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}\~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$~");
                      |                                                                                                                          |
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274