5.10 Validating an E-mail Address


You want to do simple validation on an e-mail address.

Technique

Use the following regular expression. (The double escapes are for double quotation marks; use single escapes for single quotation marks.)

 ^([a-z0-9_]\-\.)+@(([a-z0-9_]\-)+\.)+[a-z]{2,4}$ 

Comments

To call this code, you must use eregi () . ( ereg() will not work because the search needs to be case-insensitive.) Here is how you might call this code in a Web page:

 <?php if (!eregi ("^([a-z0-9_]\-\.)+@(([a-z0-9_]\-)+\.)+[a-z]{2,4}$", $email))     die ("Invalid E-mail"); ?> 

The previous regular expression is a PHP-compatible global e-mail-checking regular expression, but it is not the most secure way of checking an e-mail address. For example, somebody@something-foo.cde would be validated because it is in the correct format, but it is obviously not a valid e-mail address.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net