简体   繁体   English

表单验证。 要求用户输入“确定”才能继续

[英]Form validation. Require the user to enter “OK” to continue

I am working on a legacy PHP application on CakePHP 2.0.5. 我正在CakePHP 2.0.5上的旧版PHP应用程序上工作。 There is a text field, and I need the user to type in the word "OK" to proceed. 有一个文本字段,我需要用户输入单词“ OK”才能继续。

<?php echo $this->Form->create('Transaction', array('url' => array('controller' => 'transactions', 'action' => 'go_proceed'), 'id' => 'form-add')); ?> 

<?php echo $this->Form->input("validate_ok", array("div" => false, "label" => false, 'class' => 'form-control', 'required'=>true)); ?> 

<?php echo $this->Form->end(); ?>

Currently, if the user didn't enter anything, and clicked on the submit button, a message will come up saying "Please fill out this field". 当前,如果用户未输入任何内容,然后单击“提交”按钮,则会出现一条消息,提示“请填写此字段”。

How can I also validate that if the user has entered "OK"? 如果用户输入了“确定”,我还如何验证呢? If the user has entered "OK" and clicked on the submit button, I will allow him to submit the form, if not, I want to display the message "Please enter OK to proceed". 如果用户输入了“确定”并单击了提交按钮,我将允许他提交表单,否则,我将显示消息“请输入确定以继续”。

Add 'pattern' => 'REGEX_PATTERN' in your input as follow: 在输入中添加'pattern' => 'REGEX_PATTERN' ,如下所示:

<?= $this->Form->input('validate_ok', [
   'div' => false, 
   'label' => false, 
   'class' => 'form-control', 
   'required' => true,
   'pattern' => 'OK|ok',
]); ?> 

https://www.w3schools.com/tags/att_input_pattern.asp https://www.w3schools.com/tags/att_input_pattern.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM