regex for email validation – There is a several PHP programs. Get contacted by someone that is having trouble with a site that uses it, and we end up having to make some adjustment (most recently we realized that wasn’t allowing 4-character TLDs).

For PHP, we should not use the pattern (regex for email validation) given in Validate an E-Mail Address with PHP, the Right Way.

  • That is no better than all the other non-RFC patterns. It isn’t even smart enough to handle even RFC 822, let alone RFC 5322.
  • If we need to get visualize and sophistic, implement a complete state engine. A regular expression can only act as a basic filter. The problem with regular expressions is that telling someone that their perfectly valid e-mail address is invalid (a false positive) because our regular expression can’t handle it is just offensive and impolite from the user’s perspective.
  • A state engine for the purpose can both validate and even correct e-mail addresses that would otherwise be considered invalid as it disassembles the e-mail address according to each RFC.

  • We should not use regular expressions to validate email addresses.
  • Instead, use the MailAddress class, like this:
[pastacode lang=”php” manual=”try%20%7B%0A%20address%20%3D%20new%20MailAddress(address).Address%3B%20%7D%20catch(FormatException%0A%7B%20%2F%2Faddress%20is%20invalid%0A%20%7D%20%0A” message=”email address code” highlight=”” provider=”manual”/]
  • The MailAddress class uses a BNF parser to validate the address in full accordance with RFC822.

why we need to validate email addresses? What is the real benefit for validating?

  • It will not catch common typos.
  • It does not prevent people from entering invalid or made-up email addresses, or entering someone else’s address.

If we need to validate that an email is correct, we have no choice than to send an confirmation email and have the user reply to that. In many cases we have to send a confirmation mail anyway for security reasons or for ethical reasons

  • If we need to use the regular expression, there’s two things we need to understand.
  • First, long regex for email validation make it difficult to nicely format paragraphs. So we don’t include a-z in any of the three character classes. This regex is intended to be used with our regex engine’s “case insensitive” option turned on.
  • Second, the above regex is delimited with word boundaries, which makes it suitable for extracting email addresses from files or larger blocks of text. If we need to check whether the user typed in a valid email address, replace the word boundaries with start-of-string and end-of-string anchors, like this: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$.
[ad type=”banner”]

  • Email Regular Expression Pattern
[pastacode lang=”php” manual=”%5E%5B_A-Za-z0-9-%5C%5C%2B%5D%2B(%5C%5C.%5B_A-Za-z0-9-%5D%2B)*%0A%20%20A-Za-z0-9-%5D%2B(%5C%5C.%5BA-Za-z0-9%5D%2B)*(%5C%5C.%5BA-Za-z%5D%7B2%2C%7D)%24%3B%20%0A” message=”email address code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%5E%20%23start%20of%20the%20line%0A%20%5B_A-Za-z0-9-%5C%5C%2B%5D%2B%20%23%20must%20start%20with%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%231%20%0A%5C%5C.%5B_A-Za-z0-9-%5D%2B%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20)*%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%231%2C%20this%20group%20is%20optional%20(*)%0A%20%40%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20must%20contains%20a%20%22%40%22%20symbol%0A%20%5BA-Za-z0-9-%5D%2B%20%23%20follow%20by%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%232%20-%20first%20level%20TLD%20checking%20%0A%5C%5C.%5BA-Za-z0-9%5D%2B%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%0A%20(%2B)%0A%20)*%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%232%2C%20this%20group%20is%20optional%20(*)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%233%20-%20second%20level%20TLD%20checking%20%0A%5C%5C.%5BA-Za-z%5D%7B2%2C%7D%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20with%20minimum%20length%20of%202%0A%20)%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%233%20%24%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23end%20of%20the%20line%0A” message=”email address code” highlight=”” provider=”manual”/] [ad type=”banner”]

Examples:
venkat@ gmail.com (spaces in emails) or lavanya (no domain at all) or lavs@gmailcom (no period before .com),

[pastacode lang=”php” manual=”%2F%5E%5CS%2B%40%5CS%2B%5C.%5CS%2B%24%2F%20%0A” message=”php validate email code” highlight=”” provider=”manual”/]

Context:

[pastacode lang=”php” manual=”%5E%5Ba-zA-Z0-9.!%23%24%25%26’*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B%40%5Ba-zA-Z0-9%5D(%3F%3A%5Ba-zA-Z0-9-%5D%7B0%2C61%7D%5Ba-zA-Z0-9%5D)%3F(%3F%3A%5C.%5Ba-zA-Z0-9%5D(%3F%3A%5Ba-zA-Z0-9-%5D%7B0%2C61%7D%5Ba-zA-Z0-9%5D)%3F)*%24%20%0A” message=”email address code” highlight=”” provider=”manual”/]

Note: E-mail addresses that is simultaneously too strict (before the “@” character), too unclear (after the “@” character), and too negligent (allowing comments, whitespace characters, and quoted strings in manners unfamiliar to most users).

The one which is used in ASP.NET by the RegularExpressionValidator.

[pastacode lang=”php” manual=”%5E%5Cw%2B(%5B-%2B.’%5D%5Cw%2B)*%40%5Cw%2B(%5B-.%5D%5Cw%2B)*%5C.%5Cw%2B(%5B-.%5D%5Cw%2B)*%24%20%0A” message=”php validate email code” highlight=”” provider=”manual”/]

The email addresses validation using ASP.NET web application using the System.Net.Mail namespace to send emails to a list of people. So, rather than using some very complex regular expression, Simply try to create a MailAddress instance from the address. The MailAddress constructor will throw an exception if the address is not formed properly.

[pastacode lang=”php” manual=”protected%20void%20emailValidator_ServerValidate(object%20source%2C%20ServerValidateEventArgs%20args)%20%0A%7B%20%0Atry%20%0A%7B%20%0Avar%20a%20%3D%20new%20MailAddress(txtEmail.Text)%3B%20%0A%7D%20%0Acatch%20(Exception%20ex)%0A%7B%0Aargs.IsValid%20%3D%20false%3B%0AemailValidator.ErrorMessage%20%3D%20%22email%3A%20%22%20%2B%20ex.Message%3B%20%7D%20%7D%20%0A” message=”php validate email code” highlight=”” provider=”manual”/]

  • Use the following regex for email validation for input validation:
[pastacode lang=”php” manual=”(%5B-!%23-‘*%2B%2F-9%3D%3FA-Z%5E-~%5D%2B(%5C.%5B-!%23-‘*%2B%2F-9%3D%3FA-Z%5E-~%5D%2B)*%7C%22(%5B%5D!%23-%5B%5E-~%20%5Ct%5D%7C(%5C%5C%5B%5Ct%20-~%5D))%2B%22)%40%5B0-9A-Za-z%5D(%5B0-9A-Za-z-%5D%7B0%2C61%7D%5B0-9A-Za-z%5D)%3F(%5C.%5B0-9A-Za-z%5D(%5B0-9A-Za-z-%5D%7B0%2C61%7D%5B0-9A-Za-z%5D)%3F)%2B” message=”php validate email code” highlight=”” provider=”manual”/] [ad type=”banner”]

Addresses matched by this regex for email validation:

  • Have a local part (i.e. the part before the @-sign) that is strictly compliant with RFC 5321/5322,
  • Have a domain part (i.e. the part after the @-sign) that is a host name with at least two labels, each of which is at most 63 characters long.

The second constraint is a restriction on RFC 5321/5322

  • Use the PHP build-in validation for emails.
[pastacode lang=”php” manual=”filter_var(%24value%2C%20FILTER_VALIDATE_EMAIL)%20%0A” message=”php validate email code” highlight=”” provider=”manual”/]
  • If we’re running a php-version lower than 5.3.6 please be aware of this issue: https://bugs.php.net/bug.php?id=53091
  • This article provides some of the basic informations on email address , validate email , php validate email , php email validation , email tester , email validation javascript , email validation php , php email , php form validation , emailvalidator , php validator , validation for email , form validation php , email validation regex , form validation in php , validation in php , email validation in php , regular expression for email , how to validate email in javascript , php contact form with validation , regular expression in php , email verification in php.

Categorized in: