PHP Form Validation

Source: Internet
Author: User

Static mode: The validation rule is defined in the model class through the $_validate attribute.

InfoModel.class.php

<?phpnamespace Home\model; UseThink\model;classInfomodelextendsmodel{protected $_validate=Array(        //Array (Validation of fields, validation rules, error prompts, [validation conditions, additional rules, validation time])//non-null validation regex        Array("Code", "Require", "codename cannot be empty", 0, "regex", 3),//Verify that the field is unique        Array("Name", "", "name cannot be duplicated", 0, "unique", 3),//verify that two times the input is consistent confirm        Array("Queren", "Sex", "two times input inconsistency", 0, "confirm", 3),//verifies that the input is consistent with the value defined by the regular equal        Array("Queren", "123", "incorrect input", 0, "equal", 3),//scope validation between        Array("Queren", "18,30", "Enter age between 18~30岁", 0, "between", 3),//Email Email        Array("Queren", "email", "email format entered incorrectly", 0, "regex", 3),    );}
functionTest () {$model=d ("Info"); $model-Create (); if(!$model-Create ()) {    Echo $model-GetError (); }    Else    {    $bs=$model-Add (); //jump page//1. After successful jump: Success ("Prompt Words", "How to jump", the length of the wait)//2. Jump after failure: error ("Prompt", "default skip back to Previous page")    if($bs)    {        $this->success ("Add Success", "Test"); }    Else    {        $this->error ("Add Failed"); }   }}

Dynamic: Use the Validate method of the model class to dynamically create automatic validation rules.

functionTest () {$model=d ("Info"); $model-Create (); $rules=Array(    Array("Code", "Require", "codename cannot be empty", 0, "regex", 3),        ); if(!$model->validate ($rules),Create ()) {    Echo $model-GetError (); }    Else    {    $bs=$model-Add (); //jump page//1. After successful jump: Success ("Prompt Words", "How to jump", the length of the wait)//2. Jump after failure: error ("Prompt", "default skip back to Previous page")    if($bs)    {        $this->success ("Add Success", "Test"); }    Else    {        $this->error ("Add Failed"); }    }}

Ajax

Test.html

<load href= "__public__/js/jquery-1.11.2.min.js"/>
<body><input id= "Code" type= "text" name= "code"/><input id= "btn" type= "button" value= "View name"/></ Body>$ (document). Ready (function(e) {$ ("#btn"). Click (function(){ varcode=$ ("#code").Val (); $.Ajax ({URL: "Chuli",Data: {Code:code},type: "POST",DataType: "TEXT",Success:function(data) {alert (data); } }) });});
</script>
function Chuli () {    $code=$_post["Code"];     $model=d ("Info");     $aname=$model->find ($code);     $name=$aname["name"];     // return to Ajax, $this->ajaxreturn (the returned value, the type of the value returned);    $this->ajaxreturn ($name, "eval");}

Test.html

<load href= "__public__/js/jquery-1.11.2.min.js"/><body><form action= "__ACTION__" method= "POST" > <div> code: <input id= "dh" type= "text" name= "code"/></div> <span id= ' Dhyz ' ></span> <di V> Name: <input type= "text" name= "name"/></div> <div> Sex: <input type= "text" name= "Sex"/></ div> <div> Confirm: <input type= "text" name= "Queren"/></div> <div> Ethnicity: <input type= "text" name = "Nation"/></div> <div> Birthday: <input type= "text" name= "Birthday"/></div> &LT;DIV&GT;&LT;INP UT type= "Submit" value= "Submission"/></div></form></body><script type= "Text/javascript" >$ (document). Ready (function(e) {$ ("#dh"). Blur (function(){                varcode=$ (This).Val (); $.Ajax ({URL: "Yanzheng",Data: {Code:code},type: "POST",DataType: "TEXT",Success:function(data) {if(Data.Trim() = = "OK")                    {                        $("#dhyz"). HTML ("Validate through"); $("#dhyz"). CSS ("Color", "green")); }                    Else                    {                        $("#dhyz").HTML (data); $("#dhyz"). CSS ("Color", "red"); }                }            })        })});</script>
functionYanzheng () {$model=d ("Info"); $str=""; $rules=Array(    Array("Code", "Require", "codename cannot be empty", 0, "regex", 3),    ); if(!$model->validate ($rules),Create ()) {        $str=$model-GetError (); }    Else    {        $str= "OK"; }    $this->ajaxreturn ($str, "eval");}

PHP Form Validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.