$errors = array();
if (preg_match('/^[a-z\d ]{3,20}$/i', $name)) {
$errors[] = "Please enter valid name.";
}
if (preg_match('/^[a-z\d ]{3,20}$/i', $category)) {
$errors[] = "Please enter valid category.";
}
if (preg_match('/^[a-z\d ]{3,20}$/i', $amount)) {
$errors[] = "Please enter valid amount.";
}
if(!empty($errors))
{
echo "The was an error filling out the form:<br><ul>";
foreach($errors as $msg)
{
echo "<li>$msg</li>";
}
echo "</ul>Please try again.";
}
或者,为了更简洁,使用类似Ananth 的答案。
// your 3 fields that need to be validated; Key is field, Value is error message if check invalid.
$validate = array("name" => "Please enter valid name.", "category" => "Please enter a real category.", "amount" => "Please enter an amount.");
$error = array();
foreach ($validate as $field => $message) {
if (preg_match('/^[a-z\d ]{3,20}$/i', $$field)) {
$error[] = $message;
}
}
if(!empty($errors))
{
echo "The was an error filling out the form:<br><ul>";
foreach($errors as $msg)
{
echo "<li>$msg</li>";
}
echo "</ul>Please try again.";
}
更新
看看每个检查如何在正则表达式上进行,第一个例子很容易解决。至于第二个例子,它只需要一个小的改动。
// your 3 fields that need to be validated;
// Key is Regex, value is array with the variable name (without the $) as the key,
// and the error message as the value.
$validate = array(
'/^[a-z\d ]{3,20}$/i' => array("name" => "Please enter valid name."),
'/^[a-z\d ]{3,20}$/i' => array("category" => "Please enter a real category."),
'/^[a-z\d ]{3,20}$/i' => array("amount" => "Please enter an amount.")
);
$error = array(); // Empty array to store errors in.
foreach ($validate as $regex => $data) { // Key = $regex, Value = array($variable, $error)
if (preg_match($regex, ${$data[0]})) { // This code is untested, so try it without the braces around $data[0]
$error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
}
}
if(!empty($errors)) // If the $errors array isn't empty (has an error in it)
{
echo "The was an error filling out the form:<br><ul>";
foreach($errors as $msg) // Goes through each error, printing it to an unordered list.
{
echo "<li>$msg</li>";
}
echo "</ul>Please try again.";
}
请注意,与上面的示例相比,每个示例都具有相同的示例正则表达式,但根据您的需要更改它们很简单。
编辑
上面的所有代码都未经测试,尽管它应该可以工作,如果没有,请尝试删除 周围的大括号$data[0]
,如随附评论中所述。
更新 2
如果您需要添加可选检查器,可以稍微修改相同的代码,使用额外的foreach
循环,以检查所有可选字段。
// your 3 fields that need to be validated;
// Key is Regex, value is array with the variable name (without the $) as the key,
// and the error message as the value.
$required = array(
'/^[a-z\d ]{3,20}$/i' => array("name" => "Please enter valid name."),
'/^[a-z\d ]{3,20}$/i' => array("category" => "Please enter a real category."),
'/^[a-z\d ]{3,20}$/i' => array("amount" => "Please enter an amount.")
);
$optional = array(
'/^[a-z\d ]{3,20}$/i' => array("shipping" => "Please enter valid shipping location."),
'/^[a-z\d ]{3,20}$/i' => array("options" => "Please enter an clean option.")
);
$error = array(); // Empty array to store errors in.
foreach ($required as $regex => $data) { // Key = $regex, Value = array($variable, $error)
if (preg_match($regex, $$data[0])) { // This code is untested, so try it with or without the braces around $data[0]
$error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
}
}
foreach($optional as $regex => $data)
{
if(strlen(trim($$data[0])) > 0) // If the trimmed length of the string (all leading and trailing whitespace removed) == 0?
{
if (preg_match($regex, $$data[0])) { // This code is untested, so try it with or without the braces around $data[0]
$error[] = $data[1]; // If the data matches the regular expression provided, add the provided error message to the $error array.
}
}
if(!empty($errors)) // If the $errors array isn't empty (has an error in it)
{
echo "The was an error filling out the form:<br><ul>";
foreach($errors as $msg) // Goes through each error, printing it to an unordered list.
{
echo "<li>$msg</li>";
}
echo "</ul>Please try again.";
}