我在jquery.validate中使用bootstrap。
我在同一页面上有2个表单,所以即时通讯使用验证组 小提琴:http://fiddle.jshell.net/N5BYk/5/
小组验证成功,但我需要对外观进行一些改进:
HTML:
<form id="form1" runat="server">
<div class="validationGroup form-group">
<label for="Username">Email:</label>
<input type="text" id="Username" class="required email form-control" value="" />
<label for="Password">Password:</label>
<input type="password" id="Password" class="required form-control" value="" />
<button id="login" class="causesValidation">submit</button>
</div>
<br/>
<br/>
<br/>
<div class="validationGroup form-group">
<label for="Username">Name:</label>
<input type="text" id="FirstName" class="required form-control" value="" />
<button id="Order" class="causesValidation">submit</button>
</div>
</form>
CSS:
label {
clear: both;
float: left;
line-height: 24px;
margin-top: 10px;
padding-right: 10px;
text-align: right;
width: 100px;
vertical-align: middle;
}
label.error {
clear: none;
color: Red;
float: left;
padding-left: 10px;
white-space: nowrap;
}
使用Javascript:
$(document).ready(function () {
$("#form1").validate({
onsubmit: false
});
$('.validationGroup .causesValidation').click(Validate);
});
function Validate(evt) {
var $group = $(this).parents('.validationGroup');
var isValid = true;
$group.find(':input').each(function (i, item) {
if (!$(item).valid()) {
$(this).closest('.validationGroup').addClass('has-error');
isValid = false;
}
});
if (!isValid) evt.preventDefault();
}