$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.";
}
アップデート
各チェックが正規表現でどのように行われるかを見ると、最初の例は簡単に解決できます。2番目の例については、わずかな変更のみが必要です。
// 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]
、付随するコメントに記載されているように、中かっこを削除してみてください。
UPDATE 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.";
}