The use of jquery encapsulated JS file to form validation, improve the efficiency of web development. I have written an example of validation to show you.
The validation methods included in the example are not exhaustive, assuming that no one wants to pass
Baidu Search key: jquery.validate.js form validation help document for review.
Introduction of JS file:
<script type= "Text/javascript" src= "Js/jquery-1.11.2.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.form.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.validate.js" ></script>
<script type= "Text/javascript" src= "Js/usercenter.js" ></script>
Form form for login interface:
<form class= "Form-horizontal" method= "post" id= "LoginForm" >
<div id= "Iyoutingche-formgroup" >
<label for= "InputEmail3" class= "col-sm-2 control-label" > Email </label>
<div class= "Col-sm-10" >
<input type= "text" class= "Form-control" name= "User_email" value= "${cookie.email.value}" id= "email" placeholder= " Email ">
</div>
</div>
<div id= "Iyoutingche-formgroup" >
<label for= "InputPassword3" class= "col-sm-2 Control-label" >password</label>
<div class= "Col-sm-10" >
<input type= "Password" class= "Form-control" value= "${cookie.password.value}" Name= "User_password" id= "password" Placeholder= "Password" >
</div>
</div>
<div id= "Form-group" >
<div class= "Col-sm-offset-2 col-sm-10" >
<div class= "checkbox" >
<label>
<input type= "checkbox" Name= "Isauto" value= "1" checked= "checked" > Remember password
</label>
</div>
</div>
</div>
<div class= "Form-group" >
<div class= "Col-sm-offset-2 col-sm-10" >
<span id= "Info" ></span>
<button type= "Submit" class= "btn btn-default btn-lg" id= "loginbtn" > Login </button>
<a href= "" > No registration? </a>
</div>
</div>
</form>
Usercenter.js file:
$ ("#LoginForm"). Validate ({
rules:{
User_email: {
Required:true,
Email:true
},
user_password:{
Required:true,
Minlength:5,
},
},
messages:{
user_email:{
Required: "Please enter your mailbox",
Email: "Please enter the correct email address"
},
user_password:{
Required: "Please enter password",
Minlength:jQuery.validator.format ("Enter characters not less than 5 digits"),
},
},
Submithandler:function (form) {
$ (form). Ajaxsubmit ({
URL: ' Usercheckloginser ',
Type: ' POST ',
Success:function (data) {
if (data== "true") {
var email = $ ("#email"). Val ();
Set cookies
$.cookie (' CookieName ', Email,{expires:7,path: '/', secure:false,raw:false});
$.cookie (' CookieName ', email,{expires:7});
Cookiesave = $.cookie (' cookiename ', email);
Location.reload ();
}else{
$ ("#info"). Append ("<b> error username or password</b><br/>");
}
}
});
}
});
Note When you use Ajaxsubmit to submit a form form to the background, it is. Be sure to introduce the Jquery.form.js file, otherwise the form will not be submitted. This is not written in other blogs, it is found by themselves.
Jquery.validate.js Form Validation