When a user submit forms,i’m checking empty validation and data validation.
For ex:In data ,first field is name,i’ll check whether user has entered any other characters else than alpha character’s.if he has entered then will show him an error message.Name should contain minimum of 4 characters and max of 20 characters
I’m using this code but it is not working correctly.How to check the regex.
$validate = array("Category"=>"$productCategory", "Name" => "$productName");
$error = '';
foreach ($validate as $key => $field) {
if (preg_match('/^[a-z\d ]{4,20}$/i', $$field)) {
echo $error .= $field;
}
}
Thanks in advance!
You have a typo in the preg_match, you type
$$field(2x$) instead of$field, your regex is fine it will match:Update code to answer @Andrius Naruševičius comment