Bootstrap+jquery.validate Implementation Form Verification Instance tutorial

Source: Internet
Author: User
Tags button type

Bootstrap contains a wealth of Web components that allow you to quickly build a beautiful, full-featured Web site. These include the following components: Drop-down menus, button groups, button pull-down menus, navigation, navigation bars, breadcrumbs, pagination, typography, thumbnails, warning dialogs, progress bars, media objects, and so on, and now we talk about form validation. Project structure:



GitHub Source Address: Https://github.com/starzou/front-end-example

1, form Forms code

The code is as follows Copy Code
<! DOCTYPE html>


<html>


<head>


<title>bootstrap Form template</title>


<meta charset= "Utf-8"/>


<meta name= "viewport" content= "Width=device-width, initial-scale=1.0" >


<link rel= "stylesheet" type= "Text/css" href= "Plugins/bootstrap/css/bootstrap.css"/>


</head>


<body>


<div class= "Container" >


<h1 class= "Text-center text-danger" >form example </h1>


<form role= "form" class= "Form-horizontal" action= "Javascript:alert (' authentication succeeded, can be submitted. ');" method= "Post >


<div class= "Form-group" >


<label class= "col-md-2 control-label" for= "name" >Name</label>


<div class= "Col-md-10" >


<input class= "Form-control" name= "name" type= "text" id= "name" placeholder= "name" value= ""/>


</div>


</div>


<div class= "Form-group" >


<label class= "col-md-2 control-label" for= "ExampleInputPassword1" >Password</label>


<div class= "Col-md-10" >


<input type= "password" name= "password" class= "Form-control" id= "exampleInputPassword1" placeholder= "password" >


</div>


</div>


<div class= "Form-group" >


<label for= "Intro" class= "Control-label col-md-2" >Intro</label>


<div class= "Col-md-10" >


<textarea id= "Intro" class= "Form-control" rows= "3" name= "Intro" placeholder= "Intro" ></textarea>


</div>


</div>





<div class= "Form-group" >


<label class= "Control-label col-md-2" >Gender</label>


<div class= "Col-md-10" >


<label class= "Radio-inline" >


<input type= "Radio" name= "Gender" value= "male"/>


Boy </label>





<label class= "Radio-inline" >


<input type= "Radio" name= "Gender" value= "female"/>


Gril </label>


</div>


</div>





<div class= "Form-group" >


<label for= "Hobby" class= "Control-label col-md-2" >Hobby</label>


<div class= "Col-md-10" >


<div class= "checkbox" >


<label>


<input type= "checkbox" name= "Hobby" value= "Music" >


Music</label>


</div>


<div class= "checkbox" >


<label>


<input type= "checkbox" name= "Hobby" id= "value=" Game "/>"


Game </label>


</div>





<label class= "Checkbox-inline" >


<input type= "checkbox" id= "InlineCheckbox1" value= "Option1" >


Option1 </label>


<label class= "Checkbox-inline" >


<input type= "checkbox" id= "InlineCheckbox2" value= "Option2" >


Option3</label>


<label class= "Checkbox-inline" >


<input type= "checkbox" id= "InlineCheckbox3" value= "Option3" >


Option3 </label>


</div>


</div>





<div class= "Form-group" >


<label for= "sel" class= "Control-label col-md-2" >Select</label>


<div class= "Col-md-10" >


<select multiple= "" id= "sel" name= "sel" class= "Form-control" >


<option value= "1" >1</option>


<option value= "2" >2</option>


<option value= "3" >3</option>


</select>


</div>


</div>





<div class= "Form-group" >


<div class= "Col-md-offset-2 col-md-10" >


<button type= "Submit" class= "btn btn-primary btn-sm" >


Submit


</button>


<button type= "reset" class= "btn btn-primary btn-sm" >


Reset


</button>


</div>


</div>


</form>


</div>





<script src= "Plugins/jquery-1.11.1.js" type= "Text/javascript" charset= "Utf-8" ></script>


<script src= "Plugins/bootstrap/js/bootstrap.js" type= "Text/javascript" charset= "Utf-8" ></script>


<script src= "Plugins/jquery-validation/dist/jquery.validate.js" type= "Text/javascript" charset= "Utf-8" >< /script>





<script src= "Scripts/form.js" type= "Text/javascript" charset= "Utf-8" ></script>


<script type= "Text/javascript" charset= "Utf-8" >


Myvalidator.init ();


</script>


</body>


</html>


2, Form.js code

The code is as follows Copy Code
var myvalidator = function () {


var handlesubmit = function () {


$ ('. Form-horizontal '). Validate ({


Errorelement: ' Span ',


Errorclass: ' Help-block ',


Focusinvalid:false,


Rules: {


Name: {


Required:true


},


Password: {


Required:true


},


Intro: {


Required:true


}


},


Messages: {


Name: {


Required: "Username is required."


},


Password: {


Required: "Password is required."


},


Intro: {


Required: "Intro is required."


}


},





Highlight:function (Element) {


$ (Element). Closest ('. Form-group '). addclass (' Has-error ');


},





Success:function (label) {


Label.closest ('. Form-group '). Removeclass (' Has-error ');


Label.remove ();


},





Errorplacement:function (Error, Element) {


Element.parent (' div '). Append (Error);


},





Submithandler:function (form) {


Form.submit ();


}


});





$ ('. Form-horizontal input '). KeyPress (function (e) {


if (E.which = = 13) {


if ($ ('. Form-horizontal '). Validate (). form ()) {


$ ('. Form-horizontal '). Submit ();


}


return false;


}


});


}


return {


Init:function () {


Handlesubmit ();


}


};





}();




Effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.