Check email address format (PHP & Javascript)

Here are two code snippets to verify email addresses, which is quite useful when you wish to validate a form with Javascript or PHP, avoiding users to write anything as email.

Verify email format with Javascript

Insert this code between <head> and </head> tags of your HTML document :



Insert a form between <body> and </body> tags :



Eventonsubmit calls checkMail() function and gives him text value of email field. Email field is identified by ID email.

Try this script online

Verify email format with PHP

This little PHP function allows you to check if the user wrote a valid email address that may exist. You call this function when you’re checking POST values after form submission.

function validMail($addr) {
if ((!ereg(“.+\@.+\..+”, $addr)) || (!ereg(“^[a-zA-Z0-9_@.-]+$”, $addr)))
RETURN FALSE;
else
RETURN TRUE;
}
// Use :
if(!validMail(‘infoaddress.com’)) {
echo “Your email address is invalid!”;
}

It won’t prove real existence of the email address, but it will avoid the user from writing stupid stuff.

Ce message est également disponible en : French

6 thoughts on “Check email address format (PHP & Javascript)

  1. it runs two php scripts when followed first adds the deny from IP to the .ht access that’s in the root below all directories {except the directory containing the error pages and php scripts for blacklisting

  2. this error is detected and i can’t fised it :(

    Deprecated: Function ereg() is deprecated in C:\Program Files\EasyPHP-5.3.3.1\www\Admin-php\phpcheck.php on line 29
    Invalid email

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>