(1) IndexController.class.php页面
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function yz(){ $u= M("users");//造一个父类对象 if(empty($_POST)){ $this->show(); }else{ $rules = array( array("uid","require","用户名不能为空!"), ); if($u->validate($rules)->create()){//验证 $this->ajaxReturn("ok","eval"); }else{ $this->ajaxReturn("no","eval"); } } } }
(2) yz.html页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="__ROOT__/Public/js/jquery-3.2.0.min.js"></script> </head> <body> <h1>验证界面</h1> <form action="__ACTION__" method="post"> <div><input type="text" name="uid" id="uid" /><span id="ts"></span></div> <div><input type="submit" value="验证" /></div> </form> </body> <script type="text/javascript"> $("#uid").blur(function(){ var uid = $(this).val(); $.ajax({ url:"__ACTION__", data:{uid:uid}, type:"POST", dataType:"TEXT", success: function(data){ if(data.trim()=="ok") { $("#ts").html("验证通过"); } else { $("#ts").html("用户名不能为空"); } } }); }) </script> </html>
看一下效果:
当文本框失去焦点时:
当文本框有内容时,再失去焦点:
以上所述是小编给大家介绍的ThinkPHP框架表单验证操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!