【问题标题】:PHP Submit Form that requires I validation AND blank/empty fields需要验证和空白/空字段的 PHP 提交表单
【发布时间】:2015-04-11 05:16:43
【问题描述】:

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

在 Ryans 的指导下,我有以下代码来处理“questions”和“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';

【问题讨论】:

    标签: php validation


    【解决方案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>';
    }
    

    【讨论】:

    • 是的。在“questions”和“administrative_questions”字段之前有两个字段用于“first_name”和“last_name”,它们是必填字段,也要求防止添加数值。我提出的代码中的错误消息是“任何错误”,无论是非法字符输入还是根本没有输入。我可以将这两个字段修改为特定于它们的验证。
    • 我编辑了问题以澄清需求和原因,并清除了我的旧代码以支持您提供给我的代码以及我的测试结果的详细信息。我希望这有帮助。谢谢!
    • 这非常有效!!谢谢你,谢谢你,谢谢你!!我现在唯一缺少的部分是前两个字段“first_name_ 和“last_name”是必需的,不能留空。我将尝试修改您提供的代码,但如果您今天早上碰巧看到这个并倾向于帮我最后一次,我将非常感谢!
    • 瑞恩文森特,你太棒了!!我能够在您的代码之前为“first_name”和“last_name”添加我的原始验证例程,并且所有工作都像魔术一样!再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2012-11-26
    相关资源
    最近更新 更多