I’ve five fields in the form
When a form is submitted and if three fields are not filled,it should validate and show errors on those three fields.
I used to do it using if loops,this will show one error at a time,instead, I want to show all the errors.
I want to check special characters,empty validation,min and max characters on each field.
preg_match('/^[a-z\d ]{3,20}$/i', $category
How to check them all at once using PHP?
Update
$errors = array();
$required = array("Name", "Email");
foreach($_POST as $key=>$value)
{
if(!empty($value))
{
$$key = $value;
}
else
{
if(in_array($key, $required))
{
array_push($errors, $key);
}
}
}
This can be used to check empty validation for all fiedls,how do i check for special characters,alpha numeric characters,the provblem would be each field will have different regex.
For eg: phone number and email can not have same regex.
Thanks in advance!
Or, to make it more concise, use something like Ananth’s answer.
UPDATE
Seeing as how each check has it’s on regex, the first example is easy enough to solve. As for the second example, it only requires a small change.
Note than in the above example, each has the same example regex, but it’s simple enough to change those to your needs.
EDIT
All the code above is untested, though it should work, if it doesn’t, try removing the braces around
$data[0], as mentioned in the accompanying comments.UPDATE 2
If you need to add an optional checker, the same code can be modified slightly, with an extra
foreachloop, for all the optional fields to check.