For the serious webmaster in you, receiving a wrongly formatted email address from your site's newsletter subscription is a pain in the neck. If only you could be there to check every subscription and validate it before heaping it up to your database.
In PHP you could always act like you're there to check by defining a valid email formatting rule like the function below:function validEmail($str){
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
To start checking, pass the value of the inputted email address from the subscription form to the function together with a simple true or false condition.if (validEmail($_POST['email'])==FALSE){
echo 'Some error message here';
}else{
/* Some SQL statement here to add the email address to your database */
}
Now, the only problem you might still bump into is a correctly formatted email address that doesn't actually exist. Tomorrow, I'll tell you how to Check if an Email Address exists.
Saturday, September 22, 2007
Validating Email Address in PHP
Posted by Nick Nichelle Dael Label: PHP
Subscribe to:
Post Comments (RSS)
No comments:
Post a Comment