基于Bootstrap+jQuery.validate實(shí)現(xiàn)Form表單驗(yàn)證
基于Bootstrap jQuery.validate Form表單驗(yàn)證實(shí)踐項(xiàng)目結(jié)構(gòu) :
github 上源碼地址:https://github.com/starzou/front-end-example
1、form 表單代碼[html]
<!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 示例</h1>
<form role="form" class="form-horizontal" action="javascript:alert('驗(yàn)證成功,可以提交.');" 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="男" />
boy </label>
<label class="radio-inline">
<input type="radio" name="gender" value="女" />
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>
需要引用jquery.js,bootstrap.js,jquery.validate.js 庫(kù)
2、form.js 代碼[javascript]
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();
}
};
}();
效果 :
相當(dāng)不錯(cuò)的一個(gè)表單驗(yàn)證的特效,這里推薦給大家,小伙伴們自由美化下就可以用到自己項(xiàng)目中了。
- jquery validate.js表單驗(yàn)證的基本用法入門(mén)
- jquery表單驗(yàn)證使用插件formValidator
- Jquery練習(xí)之表單驗(yàn)證實(shí)現(xiàn)代碼
- 使用 jQuery 實(shí)現(xiàn)表單驗(yàn)證功能
- 基于Jquery實(shí)現(xiàn)表單驗(yàn)證
- jQuery 表單驗(yàn)證插件formValidation實(shí)現(xiàn)個(gè)性化錯(cuò)誤提示
- jquery實(shí)現(xiàn)表單驗(yàn)證并阻止非法提交
- jQuery實(shí)現(xiàn)用戶(hù)注冊(cè)的表單驗(yàn)證示例
- Jquery插件easyUi表單驗(yàn)證提交(示例代碼)
- jQuery實(shí)現(xiàn)表單驗(yàn)證功能
相關(guān)文章
LazyForm jQuery plugin 定制您的CheckBox Radio和Select
LazyForm jQuery plugin 定制您的CheckBox Radio和Select ,需要的朋友可以參考下。2009-10-10jQuery焦點(diǎn)圖切換特效插件封裝實(shí)例
這篇文章介紹了jQuery焦點(diǎn)圖切換特效插件封裝實(shí)例,有需要的朋友可以參考一下2013-08-08jQuery深拷貝Json對(duì)象簡(jiǎn)單示例
這篇文章主要介紹了jQuery深拷貝Json對(duì)象的簡(jiǎn)單實(shí)現(xiàn)方法,以簡(jiǎn)單示例形式分析了jQuery深拷貝的操作技巧,需要的朋友可以參考下2016-07-07jquery實(shí)現(xiàn)表單獲取短信驗(yàn)證碼代碼
本文主要分享了jquery實(shí)現(xiàn)表單獲取短信驗(yàn)證碼的代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03jQuery實(shí)現(xiàn)為動(dòng)態(tài)添加的元素綁定事件實(shí)例分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)為動(dòng)態(tài)添加的元素綁定事件,結(jié)合實(shí)例形式分析了jQuery常見(jiàn)的事件綁定相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-09-09Jqgrid設(shè)置全選(選擇)及獲取選擇行的值示例代碼
本篇文章主要介紹了Jqgrid設(shè)置全選(選擇)及獲取選擇行的值示例代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12