I need to validate submitted form data:
- To check whether fields are empty.
- Proceed to validate non-empty data.
x – allows only spaces, underscore, aplha numeric characters.
y – checks whether it is an image or not.
I’m using below code, it does not work. It is not validating and printing error.
<?php
$validate = array(
'/^[a-z\d ]{4,20}$/i' => array('$x' => 'Please enter valid name.'),
'/^[a-z\d ]{4,20}$/i' => array('$y' => 'Please enter a real category.')
);
$error = '';
foreach ($validate as $key => $field)
{
if (preg_match($key,$field[0]))
{
$error.= $field[0];
}
}
if ($error)
{
echo $error;
exit;
}
You’re matching your regex against your error message, not your submitted strings ($x and $y).
You probably ment to do something like this:
UPDATE
how I would do it.
$validatenow is an array of your fields, each expressed with it’s own array containing error message, regex to match it against and submitted subject.