0

我在表单中有五个字段

提交表单时,如果三个字段未填写,则应验证并在这三个字段上显示错误。

我曾经使用 if 循环来执行此操作,这将一次显示一个错误,相反,我想显示所有错误。

我想检查每个字段上的特殊字符、空验证、最小和最大字符。

      preg_match('/^[a-z\d ]{3,20}$/i', $category

如何使用 PHP 一次检查它们?

更新

 $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);
     }
    }        

   }

这可用于检查所有字段的空验证,我如何检查特殊字符、字母数字字符,provblem 将是每个字段将有不同的正则表达式。例如:电话号码和电子邮件不能有相同的正则表达式。

提前致谢!

4

2 回答 2

0
$validate = array("name", "category", "amount"); // your 3 fields that need to be validated

$error = '';
foreach ($validate as $field) {
    if (preg_match('/^[a-z\d ]{3,20}$/i', $$field)) {
        $error .= $field;
    }
}

稍后,基于 $error 你可以显示你的错误。

于 2012-06-15T09:50:00.013 回答
0
$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.";
}
于 2012-06-15T10:00:12.917 回答