Pear :: HTML

xiaoxiao2021-03-06  70

Custom verification rules:

Most of our verification rules we use QuickForm are already enough, but if there is a special data format we need? QuickForm allows you to customize the verification rules.

In the example below, we have increased a custom rules: the username must be haohappy.

CustomValidation.php

addelement ('header', 'header', 'please login'); $ FORM -> addelement ('Text', 'Name', 'User Name:'); $ FORM-> AddElement ('password', 'password', 'password:'); $ form-> addelement ('Submit', ' ',' Submit '); // Add three verification rules $ form-> addrule (' name ',' username can't be empty! ',' Required '); $ form-> addrule (' name ", 'username Must be 3 or more letters or numbers', 'minlength', 3); $ form-> addrule ('password', 'password can not be empty!', 'Required'); $ form-> registerrule ('haohappy_only', 'Function', 'Check_haohappy'); $ form-> addrule ('name "must be haohappy', 'haohappy_only');

IF ($ form-> validate ()) {$ form-> process ('sign_hello');} else {$ form-> display ();} Function Say_hello ($ data) {print 'Hello,'. $ data [ 'name']; print '
';. print 'Your password is' $ data [ 'password'];} function check_haohappy ($ element_name, $ element_value) {if ($ element_value == 'Haohappy') {return Else {RETURN FALSE;}}?>

The effect is as shown:

You should notice that we have added the following:

$ form-> registerrule ('haohappy_only', 'function', 'check_haohappy'); $ form-> addrule ('name') must be haohappy ',' haohappy_only ';

We have newly registered a validation rule called "haohappy_only" and specify this verification by function (function) check_haohpapy (). Next, we define a check_haohpaPy () function, return TRUE when the user is named haohappy, and returns false when HaohappY is not.

Function Check_haohappy ($ Element_name, $ Element_Value) {if ($ ELEMENT_VALUE == 'Haohappy') {Return True;} else {returnaf false;}}

转载请注明原文地址:https://www.9cbs.com/read-120168.html

New Post(0)