简体   繁体   中英

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

I am working on a legacy PHP application on CakePHP 2.0.5. There is a text field, and I need the user to type in the word "OK" to proceed.

<?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:

<?= $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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