动态验证:(不需要建Model模型)
1.建一个控制器,做表单操作(包含验证)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php namespace Biaodan\Controller; use Think\Controller; class BiaodanController extends Controller { public function test() { if ( empty ( $_POST )) //如果$_POST空,显示添加页面, { $this ->show(); } else //如果$_POST不为空,走验证,验证是否成功,添加数据库 { $y = D( "yonghu" ); $arr = array ( //造一个验证规则 array (‘uid‘,‘ require ‘,‘用户名不能为空!‘,0), array (‘pwd‘,‘pwd1‘,‘输入的密码不一致‘,0,‘confirm‘), //相等验证:confirm(验证表单中的两个字段是否相同,定义的验证规则是一个字段名),意思是pwd1必须为字段 array (‘email‘,‘email‘,‘邮箱格式不正确‘,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)$/‘,‘身份证号不正确‘,0,‘regex‘), //正则表达式, array (‘age‘,‘18,50‘,‘年龄不在范围内‘,0,‘between‘) //范围验证 ); if ( $y ->validate( $arr )->create()) //$y->validate($arr),添加之前执行验证规则 { $y ->add(); } else { die ( $y ->getError()); } } } } |
2.对应控制器方法的html页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!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> </head> <body> <form action= "__ACTION__" method= "post" > <br /> <br /> <br /> <br /> <div>用户名:<input type= "text" name= "uid" /></div> <br /> <div>密码:<input type= "text" name= "pwd" /></div> <br /> <div>确认密码:<input type= "text" name= "pwd1" /></div> <br /> <div>姓名:<input type= "text" name= "name" /></div> <br /> <div>邮箱:<input type= "text" name= "email" /></div> <br /> <div>年龄:<input type= "text" name= "age" /></div> <br /> <div><input type= "submit" value= "添加" /></div> </form> </body> </html> |
ajax:(不需要建Model模型)
1.建一个控制器,做表单操作(包含验证)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php namespace Admin\Controller; use Think\Controller; class ZhuCeController extends Controller { public function add(){ $this ->show(); } public function addchuli(){ $y = D( "yonghubiao" ); $arr = array ( array (‘uid‘,‘ require ‘,‘用户名不能为空!!‘), //ps: require 意思是字段必须,就是不能为空 array (‘pwd‘,‘ require ‘,‘两次密码不一致!‘), array (‘name‘,‘ require ‘,‘没有填写名称!!‘), array (‘email‘,‘email‘,‘邮箱格式不正确!!‘), 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)$/‘,‘身份证不合法‘,0,‘regex‘), ); if ( $y ->validate( $arr )->create()){ $y ->add(); $this ->ajaxReturn( "注册成功" , "eval" ); } else { $this ->ajaxReturn( $y ->getError(), "eval" ); } } } |
2.对应控制器方法的html页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<!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" /> <script src= "__PUBLIC__/js/jquery-1.11.2.min.js" ></script> <title>用户注册</title> </head> <body> <br /> <br /> <br /> <div>用户名:<input type= "text" id= "uid" /></div> <br /> <div> 密码:<input type= "text" id= "pwd" /></div> <br /> <div> 名称:<input type= "text" id= "name" /></div> <br /> <div> 邮箱:<input type= "text" id= "email" /></div> <br /> <div>身份证号:<input type= "text" id= "age" /></div> <br /> <div><input type= "button" id= "dtn" value= "提交" /></div> </body> <script type= "text/javascript" > $( "#dtn" ).click( function (){ var uid = $( "#uid" ).val(); var pwd = $( "#pwd" ).val(); var name = $( "#name" ).val(); var email = $( "#email" ).val(); var age = $( "#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> </html> |
以上所述是小编给大家介绍的Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!