PHP 表單驗證

來源:互聯網
上載者:User

標籤:

靜態方式:在模型類裡面通過$_validate屬性定義驗證規則。

InfoModel.class.php

<?phpnamespace Home\Model;use Think\Model;class InfoModel extends Model{    protected $_validate=array(        //array(驗證的欄位,驗證規則,錯誤提示,[驗證條件,附加規則,驗證時間])            //非空驗證  regex        array("Code","require","代號不可為空",0,"regex",3),        //驗證欄位是否唯一  unique        array("Name","","姓名不能重複",0,"unique",3),        //驗證兩次輸入是否一致  confirm        array("Queren","Sex","兩次輸入不一致",0,"confirm",3),        //驗證輸入的內容是否與正則的定義的值一致  equal        array("Queren","123","輸入的內容不正確",0,"equal",3),        //範圍驗證  between        array("Queren","18,30","輸入年齡要在18~30歲之間",0,"between",3),        //郵箱  email        array("Queren","email","郵箱格式輸入有誤",0,"regex",3),    );}
function test(){    $model=D("Info");        $model->create();                if(!$model->create())    {    echo $model->getError();    }    else    {    $bs=$model->add();    //跳轉頁面    //1.成功後跳轉:success("提示的話","跳轉的操作方法",等待的時間長度)    //2.失敗後跳轉:error("提示的話","預設跳回上一個頁面")    if($bs)    {        $this->success("添加成功","test");    }    else    {        $this->error("添加失敗");    }   }}

動態方式:使用模型類的validate方法動態建立自動驗證規則。

function test(){    $model=D("Info");        $model->create();                $rules=array(    array("Code","require","代號不可為空",0,"regex",3),        );                if(!$model->validate($rules)->create())    {    echo $model->getError();    }    else    {    $bs=$model->add();    //跳轉頁面    //1.成功後跳轉:success("提示的話","跳轉的操作方法",等待的時間長度)    //2.失敗後跳轉:error("提示的話","預設跳回上一個頁面")    if($bs)    {        $this->success("添加成功","test");    }    else    {        $this->error("添加失敗");    }    }}

 

 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="查看姓名" /></body></html><script type="text/javascript">$(document).ready(function(e) { $("#btn").click(function(){ var code=$("#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"];    //返回到ajax,$this->ajaxReturn(返回的值,返回的值的類型);    $this->ajaxReturn($name,"eval");}

 

 

test.html

<load href="__PUBLIC__/Js/jquery-1.11.2.min.js" /><body><form action="__ACTION__" method="post">    <div>代號:<input id="dh" type="text" name="Code" /></div>    <span id=‘dhyz‘></span>    <div>姓名:<input type="text" name="Name" /></div>    <div>性別:<input type="text" name="Sex" /></div>    <div>確認:<input type="text" name="Queren" /></div>    <div>民族:<input type="text" name="Nation" /></div>    <div>生日:<input type="text" name="Birthday" /></div>    <div><input type="submit" value="提交" /></div></form></body><script type="text/javascript">$(document).ready(function(e) {$("#dh").blur(function(){                var code=$(this).val();            $.ajax({            url:"YanZheng",            data:{Code:code},            type:"POST",            dataType:"TEXT",            success: function(data){                    if(data.trim()=="OK")                    {                        $("#dhyz").html("驗證通過");                        $("#dhyz").css("color","green");                    }                    else                    {                        $("#dhyz").html(data);                        $("#dhyz").css("color","red");                    }                }            })        })});</script>
function YanZheng(){    $model=D("Info");    $str="";    $rules=array(    array("Code","require","代號不可為空",0,"regex",3),    );    if(!$model->validate($rules)->create())    {        $str=$model->getError();    }    else    {        $str="OK";        }    $this->ajaxReturn($str,"eval");}

 

PHP 表單驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.