PHP提交需要我验证和空白/空字段的表单

时间:2015-04-11 05:16:44

标签: php validation

我有一个提交表单,其中包含4个字段,由客户填充; "如first_name" "姓氏" "问题" " administrative_questions" 没有字段可以允许提交数字值,如果尝试使用输入的数字进行发布,则必须返回错误。 前两个字段是必需的。 第三个和第四个字段要求填充一个或另一个,没有数字,并且两者都可以填充,但两者都不能为空。

在Ryans的指导下,我有以下代码来处理"问题"和" administrative_questions"字段,它允许一个或另一个是空白但是如果两者都填充,一个将允许数字通过,但如果两个数字或两者都有数字和文本不会。只有一个人有文字,一个人有数字或数字,文字混合在一起。 此表格的要求要求不允许提交任何号码。 这是财务咨询小组的合规问题。客户无法通过此基于网络的提交表单提供任何类型的帐号,银行余额或财务价值,并且必须提供名字和姓氏。

$error_message = "";
$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$questionsEntered = preg_match($string_exp,$questions);

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$administrativeQuestionsEntered = preg_match($string_exp,$administrative_questions);

if ($questionsEntered && $administrativeQuestionsEntered) {
// both were entered - ok?
}
elseif ($questionsEntered || $administrativeQuestionsEntered) {
// either one was entered - ok
}
else {
// explain that at least one of them must be entered 
$error_message = 'your error message';

1 个答案:

答案 0 :(得分:1)

我没有阅读说明书,我应该......

  • error_message附加到
  • 如果输入了字段,则它们必须有效(每个字段两次检查)
  • 必须报告错误的所有字段。

这是经过测试的新代码......

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$questionsEntered = strlen(trim($questions)) > 0;
$questionsValid = preg_match($string_exp, $questions);
$questionErrorMsg = 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Questions for Barbara"</font> you entered does not appear to be Valid,<br />
or contains numbers which are not permitted.<br />
In order to assure your security, the use of numbers is prohibited.<br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';

$string_exp = "/^[\n\r\A-Za-z .,?!'-]+$/";
$adminQuestionsEntered = strlen(trim($administrative_questions)) > 0;
$adminQuestionsValid = preg_match($string_exp, $administrative_questions);
$adminQuestionsErrorMsg = 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Questions for Ray"</font> you entered does not appear to be Valid,<br />
or contains numbers which are not permitted.<br />
In order to assure your security, the use of numbers is prohibited.<br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';


if ($questionsEntered || $adminQuestionsEntered) {
   // either one or both were entered, are they Valid?

    if ($questionsEntered && !$questionsValid) {
        $error_message .= $questionErrorMsg;
    }

    if ($adminQuestionsEntered && !$adminQuestionsValid) {
        $error_message .= $adminQuestionsErrorMsg;
    }
}
else {
  // explain that at least one of them must be entered
  $error_message .= 'It appears you did not fill in all fields correctly.<br />
The <FONT COLOR="red">"Some questions must be entered"</font><br />
Please use the "Back" button below this message to return to your initial<br />
submission and correct to this information.<br />
<FORM><INPUT Type="button" VALUE="Back" onClick="history.go(-1);return true;"></FORM>';
}