Related concepts: HTML forms · PHP form validation
For normal server-side email-format validation, FILTER_VALIDATE_EMAIL is clearer and more maintainable than a custom regular expression. Format validation does not prove that the mailbox exists.
$email = trim($_POST['email'] ?? '');
if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
echo 'Valid email format';
} else {
echo 'Invalid email format';
}
We check the address format only; this does not prove the mailbox exists.
In an input field we can verify whether an email address has a valid format using PHP's built-in filter API. This is a part of php form validation we do for other entries to check proper formatted data is entered by the user or not.
<form method=post action=''>Enter any email address
<input type=text name=email value=$email>
<input type=submit value=Check>
<input type=hidden value=test name=todo>
</form>
$email=$_GET['email']; // GET method
$email=$_POST['email']; // POST method
Use filter_var($email, FILTER_VALIDATE_EMAIL) for normal server-side email-format validation instead of maintaining a custom regular expression.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| good | 22-05-2013 |
| its interestiing..!! | |
| albusaidy | 16-11-2014 |
| worked great for me.. I love the fact that the page I was working on stayed on screen, and script showed where change needed to be made. Awesome Is there a way to make sure people do not enter www. in front of their email, it happens often. | |
| John S | 17-04-2019 |
| Hello, eregi() is deprecated as of PHP 5.3 Thanks. | |