Dynamic validation: (No model models needed)
1. Build a controller to do form operation (including validation)
<?phpnamespace Biaodan\controller; UseThink\controller;classBiaodancontrollerextendscontroller{ Public functionTest () {if(Empty($_post))//if $_post is empty, the Add page appears, { $this-Show (); } Else //if $_post is not empty, go to verify, verify success, add Database { $y= D ("Yonghu"); $arr=Array(//Create a validation rule Array(' uid ', ' require ', ' username cannot be empty! ', 0),Array(' pwd ', ' pwd1 ', ' input passwords inconsistent ', 0, ' confirm '),//Equality Validation: Confirm (Verify that the two fields in the form are the same, the validation rule defined is a field name), meaning that pwd1 must be a field Array(' email ', ' email ', ' email ' is incorrect ', 0),Array(' name ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID number incorrect ', 0, ' regex '),//Regular Expressions, Array(' Ages ', ' 18,50 ', ' age not within range ', 0, ' between ')//Scope Validation ); if($y->validate ($arr)->create ())//$y->validate ($arr), add a validation rule before executing { $y-Add (); } Else { die($y-GetError ()); } } } }
2. HTML page corresponding to the Controller method:
<! DOCTYPE html Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
Ajax: (No model models needed)
1. Build a controller to do form operation (including validation)
<?phpnamespace Admin\controller; UseThink\controller;classZhucecontrollerextendsController { Public functionAdd () {$this-Show (); } Public functionAddchuli () {$y= D ("Yonghubiao"); $arr=Array( Array(' uid ', ' require ', ' username cannot be empty!! ‘),//Ps:require means that the field must be, or cannot be empty Array(' pwd ', ' require ', ' two times password inconsistent! ‘),Array(' name ', ' require ', ' not filled in the name!! ‘),Array(' email ', ' email ', ' email ' format is not correct!! ‘),Array(' Age ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID card not legal ', 0, ' regex '), ); if($y->validate ($arr),Create ()) { $y-Add (); $this->ajaxreturn ("Registered success", "eval"); }Else{ $this->ajaxreturn ($y->geterror (), "eval"); } }}
2. HTML page corresponding to the Controller method:
<! DOCTYPE html Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > $("#dtn"). Click (function(){ varUID = $ ("#uid").Val (); varPWD = $ ("#pwd").Val (); varName = $ ("#name").Val (); varemail = $ ("#email").Val (); varAge = $ ("#age").Val (); $.Ajax ({URL: "__controller__/addchuli",Data: {uid:uid,pwd:pwd,name:name,email:email,age:age},DataType: "TEXT",type: "POST",Success:function(data) {alert (data); } }); });</script>
thinkphp Framework Forms Automatic Authentication login Registration AJAX automatic Authentication login Registration