在WEB开发过程中,经常遇到需要做表单提交,良好的提示增加用户体验感。在Bootstrap UI使用表单验证,构建登录界面。
2、Bootstrap:引用相关JS和CSS
以下路径自行修改:
1 2 3
| <script src="assets/jquery/jquery.min.js"></script> <script src="assets/bootstrap/js/bootstrap.min.js"></script> <script src="assets/bootstrap-validator/js/bootstrapValidator.js"></script>
|
1 2
| <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"/> <link rel="stylesheet" href="assets/bootstrap-validator/css/bootstrapValidator.min.css"/>
|
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
| <div class="container"> <div class="col-md-4 col-md-offset-4"> <div class="panel panel-primary mt_150"> <div class="panel-heading text-center"><h4>后台管理系统</h4></div> <div class="panel-body"> <form id="login" class="form-horizontal" autocomplete="off"> <div class="form-group"> <label for="inputUserName3" class="col-sm-3 control-label">用户名</label> <div class="col-sm-9"> <input name="username" type="text" class="form-control" id="inputUserName3" placeholder="请输入用户名"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-3 control-label">密码</label> <div class="col-sm-9"> <input name="password" type="password" class="form-control" id="inputPassword3" placeholder="请输入密码"> </div> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-10"> <button type="reset" class="btn btn-default mr_50">重置</button> <button type="submit" class="btn btn-primary">登录</button> </div> </div> </form> </div> </div> </div> </div>
注: css样式自行调试,结构如此就行
|
4、Bootstrap:添加bootstrapValidator 自定义验证
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| $(function(){ $("#login").bootstrapValidator({ feedbackIcons : { valid: "glyphicon glyphicon-ok", invalid: "glyphicon glyphicon-remove", validating: "glyphicon glyphicon-refresh" }, fields: { username: { validators:{ notEmpty: { message: "用户名不能为空" }, callback :{ message : "用户名不存在" } } }, password: { validators: { notEmpty: { message: "密码不能为空" }, stringLength: { min:6, max:18, message: "密码必须是6-18个字符" }, callback: { message : "密码错误" } } } } }).on("success.form.bv",function(e){ e.preventDefault(); var $form=$(e.target); console.log(JSON.stringify($form.serialize())) $.ajax({ type: "post", url: "/employee/employeeLogin", data: $form.serialize(), dataType: "json", success: function (data) { if(data.success == true){ location.href = "/admin/" } else{ if(data.error == 1000){
$form.data("bootstrapValidator").updateStatus("username","INVALID","callback"); }else if(data.error == 1001){ $form.data('bootstrapValidator').updateStatus('password','INVALID','callback'); } } } }); }) })
|