Web前端校验之jquery.validate(三)

作为jquery校验的补充,再进行整理
除了之前说的在ready中加入校验外,还可以在html中的class中直接加入校验,如下
补充一:

<form id="testForm" name="testForm">
<input id="password" name="password" type="password" class="required"/>
<input id="confirm_password" name="confirm_password" type="password" class="required" equalTo="#password"/>
<input id="email" name="email" class="required email"/>
</form>

然后,在document的ready事件中,加入如下方法:
<script>
$(document).ready(function(){
$("#testForm").validate();
}
</script>

这样,当form被提交的时候,就会根据input指定的class来进行验证了。如果失败,form的提交就会被阻止。并且,将提示信息显示在input的后面。
如果你还想在错误信息上显示特别的样式(比如字体为红色)即可通过添加:

<style type="text/css">
#testForm label.error {
padding-left: 16px;
margin-left: 2px;
color:red;
background: url(img/unchecked.gif) no-repeat 0px 0px;
}
</style>


补充二:
可以通过event指定触发效验方式(可选值有keyup(每次按键时),blur(当控件失去焦点时),不指定时就只在按提交按钮时触发)
$(document).ready(function(){
$("#testForm").validate({
event:"keyup" || "blur"
})
})

补充三:
如果通过指定debug为true则表单不会提交只能用来验证(默认为提交),可用来调试
$(document).ready(function(){
$("#testForm").validate({
debug:true
})
})



参考:http://blog.csdn.net/kenkywu/article/details/7163968
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值