Re: Form Validation filter - Regex Q
| From: | Al | Date: | Thu, 12 Nov 2009 15:03:21 +0000 |
| Subject: | Re: Form Validation filter - Regex Q | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-299783@lists.php.net to get a copy of this message | ||
Haig Davis wrote:
Morning All, I've been figthing with this little problem for two days now, so far no luck with google and am beginning to question my own sanity. I have a application that has over one hundred forms some quite lengthy so what I'm trying to achieve rather than writing a bunch of individual sanitize statements then form validation statemenst that I could run $_POST through a foreach loop and filter the values by form class i.e.is it an emaill addreess or simply a text block with letters and numbers. The regex's alone work fine as does the foreach loop the only issue I have is the IF statement comparing $key to expected varieable names. Heres the bit of code envolved. if(isset($_POST['submit'])){Sorry about the misreading your request, earlier. Here is a function that I use. function checkEmailAddr($emailAddr) {foreach($_POST as $keyTemp => $valueTemp){ $key = mysqlclean($keyTemp); $value = mysqlclean($valueTemp); $$key = $key; $$key = $value;if($key != ("$customerServiceEmail") || ("$billingEmail") ||("$website")){if(preg_match("/[^a-zA-Z0-9\s]/", $value)){ $style = "yellow"; $formMsg = "Invalid Characters"; $bad = $key;} } if($key = ("$customerServiceEmail") || ("$billingEmail")){if(preg_match("/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/", $value)){$style = "yellow"; $formMsg = "Invalid Characters"; $bad = $key; } }}} Thanks for taking a peek. Haig
if(empty($emailAddr))
{
throw new Exception("No email address provided");
}
if(!preg_match("%\w+@%", $emailAddr))
{
throw new Exception("Email address missing mailbox name, or syntax is wrong. ");
}
if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL))
{
throw new Exception("Email address error. Syntax is wrong. ");
}
$domain = substr(strchr($emailAddr, '@'), 1);
if(!checkdnsrr($domain))
{
throw new Exception("Email address warning. Specified domain \"$domain\" appears to be invalid. Check carefully.");
}
return true;
}
Use the function like this
try{
checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]);
}
catch (Exception $e)
{
$userErrorMsg = $e->getMessage(); //Message text in check function
}