欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Bootstrap+PHP表单验证实例

程序员文章站 2022-10-06 23:37:25
简单实用的Bootstrap+PHP表单验证实例,非常适合初学者及js不熟悉者,还有ajax远程验证 js验证表单 PHP远程验证用户名 本实例下载:https://www.sucaihuo.com/php/1814.html ......

Bootstrap+PHP表单验证实例

 简单实用的bootstrap+php表单验证实例,非常适合初学者及js不熟悉者,还有ajax远程验证

 

js验证表单

 1 $(document).ready(function() { 
 2     $('#defaultform') 
 3             .bootstrapvalidator({ 
 4                 message: 'this value is not valid', 
 5                 feedbackicons: { 
 6                     valid: 'glyphicon glyphicon-ok', 
 7                     invalid: 'glyphicon glyphicon-remove', 
 8                     validating: 'glyphicon glyphicon-refresh' 
 9                 }, 
10                 fields: { 
11                     username: { 
12                         message: 'the username is not valid', 
13                         validators: { 
14                             notempty: { 
15                                 message: 'the username is required and can\'t be empty' 
16                             }, 
17                             stringlength: { 
18                                 min: 6, 
19                                 max: 30, 
20                                 message: 'the username must be more than 6 and less than 30 characters long' 
21                             }, 
22                             /*remote: { 
23                              url: 'remote.php', 
24                              message: 'the username is not available' 
25                              },*/ 
26                             regexp: { 
27                                 regexp: /^[a-za-z0-9_\.]+$/, 
28                                 message: 'the username can only consist of alphabetical, number, dot and underscore' 
29                             } 
30                         } 
31                     }, 
32                     email: { 
33                         validators: { 
34                             notempty: { 
35                                 message: 'the email address is required and can\'t be empty' 
36                             }, 
37                             emailaddress: { 
38                                 message: 'the input is not a valid email address' 
39                             } 
40                         } 
41                     }, 
42                     password: { 
43                         validators: { 
44                             notempty: { 
45                                 message: 'the password is required and can\'t be empty' 
46                             } 
47                         } 
48                     } 
49                 } 
50             }) 
51             .on('success.form.bv', function(e) { 
52                 // prevent form submission 
53                 e.preventdefault(); 
54  
55                 // get the form instance 
56                 var $form = $(e.target); 
57  
58                 // get the bootstrapvalidator instance 
59                 var bv = $form.data('bootstrapvalidator'); 
60  
61                 // use ajax to submit form data 
62                 $.post($form.attr('action'), $form.serialize(), function(result) { 
63                     console.log(result); 
64                 }, 'json'); 
65             }); 
66 });

 

php远程验证用户名

1 $username = $_post['username']; 
2  
3 echo json_encode(array( 
4     'message' => sprintf('welcome %s', $username), 
5 ));

本实例下载: