Home > PHP/MySQL Programming > Validating an email address in PHP

Validating an email address in PHP

A small tutorial and example script to validate the format of an e-mail address. This is very useful when capturing e-mail addresses from visitors.

If you’re using the e-mail address to send out important information then it’s advisable to ask the user to enter it twice, to account for misspelling.

<?php

#email address to validate
$email = "name@domain.com";

#validate format
if(!eregi("^[_&a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+
(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) {

echo 'Invalid format of email address';

}

?>

All done!

That’s it! All you need to validate the format of an e-mail address. This script doesn’t verify that the mailbox nor the domain actually exist. For more information on any of the code featured above visit www.php.net for the online manual.

For more information on web development please contact us on +44(0)1202 315285 or info@acmultimedia.co.uk. Learn more about AC Multimedia.
Bookmark and Share

PHP/MySQL Programming , ,

  1. Archive
    March 25th, 2009 at 15:25 | #1

    Very nice tutorial, I loved it and it was very helpful! It seems so easy but if I had to figure it out, it would take me a while:)

  2. Archive
    March 25th, 2009 at 15:26 | #2

    this is simple, i did the same through javascript in many sites. the best method of all thought is verification through email. that is the only method that will force users to enter their actual email adress and not a fake one!

  3. Archive
    March 25th, 2009 at 15:30 | #3

    Nice tutorial, thanks for sharing with us. Very easy to undertand.

  4. Archive
    March 25th, 2009 at 15:31 | #4

    useful tutorial. it’s a simple one. thanks for sharing it.

  5. Archive
    March 25th, 2009 at 15:31 | #5

    Great tutorial, a lot of designers miss out on this most of the time.

  1. No trackbacks yet.