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

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

  1. Slots on Net

    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. radwa

    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

  3. AIR JORDAN 9 RETRO

    Preserving Venice, which is sinking one to two millimetres each year, has been the city and the Italian governments focus, with the first stages of the going into effect in 2013. The 78 floodgates installed in the lagoon will eventually close off the three inlets during high tides, blocking them from swamping the city.

  4. Lebron Women shoes

    in Clerkenwell is a special place. Its rooms are small but big on personality. Its restaurant, Bistrot Bruno Loubet, and Atrium Bar, with alfresco drinking,Michael Kors Outlet, are superb (8688 Clerkenwell Rd; rooftop studios from 300).

  5. onlinehackland.com

    I could be a boxer or a sword fighter or a horseback [rider].
    ‘Users almost always pay with credit card; their purchase details are not removed as promised,
    and include real name and address, which is of course the most important information the users want removed.
    Researchers are looking to invest in something that they can benefit or get a cut from.

    My page hay day hack (onlinehackland.com)

Leave a Reply

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