Skip to content
Advertisement

Email validation using regular expression in PHP

I am pretty much new with regular expression. I am developing a project in PHP and i need to validate email address. After searching in this site and google i found the following regular expression says it should work best.

JavaScript

But when I use it I get error says,

JavaScript

What is wrong with this code?

Advertisement

Answer

Use this instead of a regular expression:

JavaScript

Using regular expressions to validate email addresses is not recommended as it is certainly not pretty, especially if you don’t want to exclude somebody who has a valid email address that is correct according to RFC 822 grammar.

http://www.php.net/filter_var is your best bet.

A crazy example of a regex that attempts to validate email addresses according to RFC 822 grammar:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement