I think we had a discussion similar to this before, and the conclusion was that we as programmers should not be using any advanced regex to check the syntax of an email address. After all, email validation would weed out any invalid emails, and then a crontab would come along and clear out any inactive accounts.
However, we should definitely by using a simple regex for checking the very simple structure of any given email address. Such as like:
This will prevent people from clearly not entering a valid email address - often by mistake.
Furthermore, being as sure as possible that an email address is valid would be somewhat slow, and that is surely not a favourable sacrifice. After all, email address structures can be very complicated.
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
The Following User Says Thank You to Wildhoney For This Useful Post:
A good first thing to do would be to switch from using eregi to preg_match (with the i modifier). This will improve the running time of the function to around a quarter of that of eregi! Also, this is a person preference more than anything, I don't like to see functions where a conditional is processed which determines a boolean response which is assigned to a local variable. Why not just use a single line of code? return (bool) preg_match(...);
For the regular expression itself, you've made an ok start. However, this falls victim of a hugely common bug in many "validation" checks: it would flag firstname.lastname@example.org as invalid, when it's not. You simply need to allow the + character in the appropriate place.
Since you're only checking to see if the pattern matches or not, there's no need to use capturing groups (...) so you can same some time and effort (on the parser's part) by making them non-capturing (?:...).