1

Currently the following email in the script validates even though there are no underscores indicated in the second part of the regular expression validation after the '@' sign. How do I make underscores invalid in the second part of the email?

<?php
$email = 'firstname.lastname@a_aa.bbb.co_m';
$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9]+([.][A-z0-9]+)*[.][A-z]{2,4}$/";

if (preg_match($regexp, $email)) {
    echo "Email address is valid.";
} else {
    echo "Email address is <u>not</u> valid.";
}
?>
Simon Suh
  • 10,599
  • 25
  • 86
  • 110
  • 1
    Should anyone else stumble upon this: The above regex does not correctly validate ALL real email addresses. [more info](http://www.regular-expressions.info/email.html) – mario Jan 11 '11 at 11:54
  • http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – dkarp Jan 11 '11 at 19:30

4 Answers4

2

don't use [A-z] character class. Use [a-zA-Z] instead. The character class that you're using includes, underscores, backticks, square brackets, etc.

To avoid any confusion just use [a-z] and specify i (case-i sensitive) flag for your regex:

$regexp = "/^[^0-9][a-z0-9_]+([.][a-z0-9_]+)*[@][a-z0-9]+([.][a-z0-9]+)*[.][a-z]{2,4}$/i";

Also note that your first character class: [^0-9] matches any character except ascii digits, that is any at all. That's probably not what you intended.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • +1. The idea of i is superb to avoid confusion. nice and neat. – Sachin Shanbhag Jan 11 '11 at 11:54
  • Also, even if he fixed it, the `[^0-9]` is probably an incorrect constraint. The local-part of an email address is a dot-atom, and it can begin with a digit. See http://tools.ietf.org/html/rfc5322#section-3.4.1 and http://tools.ietf.org/html/rfc5322#section-3.2.3 Moreover, there are lots of valid characters that he's omitting on the left side of the `@`... – dkarp Jan 11 '11 at 19:28
1

You probably meant a-zA-Z instead of A-z (which accepts any characters between A and z, including several non-alphabetic characters).

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
1

You have to use [a-zA-Z] instead of [A-z]. Notice in your Regex you have used Capital A to small z. There are other characters from Caps Z to small a which includes underscore I assume.

EDIT after comment in main question, given below is regex which almost validates all use-cases for email ID -

"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
1

If you have a look around here you will see that this question has been asked many many times. The short answer is that you cannot validate an email address with a regular expression.

The best you could probably do is validate that the right hand side of the @ character has an MX that will handle it. Even then there are some rules that you need to be aware of.

It used to be that you could VRFY an email address by asking the MX if it would handle the mail, but thanks to spam that's mostly been switched off years ago.

If you use something like this then you will annoy a number of your users I guarantee.

You should also use \w character classes rather than a-z as characters with accents etc tend to fail...

time4tea
  • 2,169
  • 3
  • 16
  • 21