thinkphp Form Validation

Source: Internet
Author: User

Form validation

static mode

First, you need to build a data model for the corresponding table, such as Infomodel.class. PHP builds member variables in the model build arrays add validation rules to the array by defining validation rules through the $_validate property

Format isarray(

array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),

array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),

......);

<?phpnamespace home\model;use think\model;class Infomodel extends model{    protected $_validate=array (        // Array ("Code", "Require", "codename cannot be empty", 0, "regex", 3),        //array ("name", "" "," name cannot be repeated ", 0," unique ", 3),        //array (" QueRen "," Sex "," Two entries inconsistent ", 0," confirm ", 3),        //array (" QueRen "," 123 "," incorrect input ", 0," equal ", 3),        //array (" QueRen "," 18,50 "," enter age to be between 18-50 ", 0," between ", 3),        //array (" QueRen "," email "," Mailbox format is incorrect ", 0," regex ", 3),    );}

  

Automatically collect form if (! $m->create ()) {echo $m->geterror ();} Else{echo "Validation passed";/* $bs = $m->add (); if ($bs) {$this->success ("Add Success", "Test");} else{$this->error ("Add Failed");} */}

  

Dynamic mode

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

Automatically verify that the string is treated as 0 when the form is collected

Dynamic validation            $rules =array (                Array ("code", "Require", "codename cannot be empty", 0, "regex", 3),            );            if (! $m->validate ($rules)->create ())            {                echo $m->geterror ();            }            else            {                echo "verification passed";               /* $bs = $m->add ();                if ($bs)                {                    $this->success ("Add Success", "Test");                }                else                {                    $this->error ("Add failed");                } */            }

Real-time verification with Ajax

Be aware of the three-point jquery introduction URL return

1.jQuery Introduction

<load href= "__public__/js/jquery-1.11.2.min.js"/>
<js href="/Public/Js/Common.js" />
<css href="/Public/Css/common.css" />
2.URL书写
__controller__/chuli behind the method name

3.ajax return $this->ajaxreturn ($name, "eval"); There is XML JSON eval (string) default to JSON

Ajax view names based on input code

<input id= "Code" type= "text" name= "code" ><input id= "btn" type= "button" value= "View name" ></body>< Script type= "Text/javascript" >    $ (document). Ready (function (e) {        $ ("#btn"). Click (function () {            var code=$ ("#code"). Val ();            $.ajax ({                URL: "__controller__/chuli",                Data:{code:code},                type: "POSt",                dataType: "TEXT",                Success:function (data) {                    alert (data);})    }) </script>

  

function Chuli ()    {        $code =$_post["code"];        $m =d ("info");        $attr = $m->find ($code);        $name = $attr ["name"];        $this->ajaxreturn ($name, "eval");    }

Ajax for real-time judgment

<div>        Code: <input id= "dh" type= "text" name= "code"/>        <span id= "Dhyz" ></span> </div ><script type= "Text/javascript" >    $ (document). Ready (function (e) {    $ ("#dh"). blur (function () {            var code=$ (this). Val ();            $.ajax ({                URL: "__controller__/yanzheng",                data:{code:code},//Note that this is the same as the column name when committing the root post submission                : "POSt",                dataType: "TEXT",                success:function (data) {                    if (data.trim () = = "OK")                    {                        $ ("#dhyz"). HTML (" Verified by ");                        $ ("#dhyz"). CSS ("Color", "green");                    }                    Else                    {                        $ ("#dhyz"). HTML (data);                        $ ("#dhyz"). CSS ("Color", "Red"),}})})    </script>

  

function Yanzheng ()    {        $m =d ("info");        $str = "";        $rules =array (            "code", "Require", "codename cannot be empty", 0, "regex", 3)        ;        if (! $m->validate ($rules)->create ())        {            $str = $m->geterror ();        }        else        {            $str = "OK";        }        $this->ajaxreturn ($str, "eval");    }

  

thinkphp 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.