How to Validate Email Addresses in PHP code?

Want create site? Find Free WordPress Themes and plugins.

Email Addresses are Easy to Create. Difficult to type.

Many thing goes wrong Wrong spell, Syntax error, missing . [DOT] or missing @

Whatever the case if user submits wrong syntax or you receive some Spam via Crawlers and Bots.

If you do not have PHP Validation Script you gonna receive bunch of crap daily.

I recently published  JARGON of Email Verification, Validation, List Cleaning & Email Hygiene If you are confused about all three just give it a read.

Getting email addresses you collect — for a newsletter, say, or for password retrieval — to at least conform to standards (if not ensure ) is crucial, of course, and extremely tricky.

Fortunately, PHP (5 and later) comes with a handy set of functions and filters that make testing for email address validity a snap.

Validate Email Addresses in a PHP Script

To validate an email address for correctness (not checking whether the address is actually working and read) in PHP:

  • Build email validation into the HTML if you use a web form where people enter email addresses.
  • Use the FILTER_VALIDATE_EMAIL PHP email validation filter. (See below for examples.)

FILTER_VALIDATE_EMAIL PHP Email Address Validation Caveats

Note that FILTER_VALIDATE_EMAIL will validate email addresses that contain domains and top-level domains that do not exist. If you want to avoid these, you can test for top-level domains that are more than 4 characters long (which will erroneously throw out “.museum”), or for domain names that are either 2 characters long (all the country top-level domains) or one of the known top-level domains (which you will have to update as the list changes).

FILTER_VALIDATE_EMAIL will erroneously balk at email addresses with long domain names (64 characters or more), and at email addresses with escaped characters (such as “me\”@example.com“).

To avoid these false positives, you can turn to a class like php-email-address-validation.

FILTER_VALIDATE_EMAIL Email Address Validation Examples

Assuming $email_address holds the address to be checked, you could try its validity using:

<?php
$email_address = "me@example.com";
if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
  // The email address is valid
} else {
  // The email address is not valid
}
?>

You can also filter an email address straight from the web form (assuming the email address was captured in field with the name “email”):

<?php
$email_address = filter_input(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
if (!$email_address) {
  // The email is not valid
}
?>

Host & Soft offers Real Time Email Validation Service which is complete Email Validator. Where Email Marketers can upload Email List in Excel file or text file. Developer can utilize its Robust functionality via Email Validation API.

This is the complete Email Validation tool which check Email address in real time without sending any Email in actual.

To check for bounced emails which no longer accepts email with many reasons our email validation app can tell the actual cause of email bounce.

Did you find apk for android? You can find new Free Android Games and apps.

More from our blog

See all posts
No Comments