Nice Jquery Validator 【从 jQuery Validation 迁移】
1. 初始化表单验证
.validate
VS .validator
jquery-validation : $("#myform").validate(options)
nice-validator: $("#myform").validator(options)
// 或者 DOM 传参,不需要初始化
2. 设置验证规则
rules
VS fields
jquery-validation 使用 rules 参数设置字段规则
$("#myform").validate({
rules: {
name: "required",
email: {
required: true,
email: true
},
contact: {
required: true,
email: {
depends: function(element) {
return $("#contactform_email").is(":checked");
}
}
},
pay_what_you_want: {
required: true
min: {
param: 15,
depends: function(element) {
return $("#bonus-material").is(":checked");
}
}
}
}
});
nice-validator 使用 fields 参数设置字段规则
$("#myform").validator({
fields: {
name: "required",
email: "required; email",
contact: "required(#contactform_email:checked); email",
pay_what_you_want: "required(#bonus-material:checked); length(15~)"
}
});
3. 设置规则消息
messages[name].rule
VS fields[name].msg
jquery-validation 使用 messages 配置消息
$("#myform").validate({
rules: {
name: {
required: true,
minlength: 2
}
},
messages: {
name: {
required: "We need your email address to contact you",
minlength: jQuery.validator.format("At least {0} characters required!")
}
}
});
nice-validator 直接在 fields 里面配置消息
$("#myform").validator({
fields: {
name: {
rule: "required; length(2~)",
msg: {
required: "We need your email address to contact you",
length: "At least {1} characters required!"
}
}
}
});
nice-validator 也支持 messages 参数,是针对规则的通用配置
$("#myform").validator({
fields: {
name: "required; length(2~)",
email: "required; email"
},
messages: {
required: "请填写{0}"
}
});
4. 自定义规则
$.validator.addMethod
VS $.validator.config
jquery-validation
// 自定义全局规则
nice-validator
$.validator.addMethod( "integer", function( value, element ) {
return this.optional( element ) || /^-?\d+$/.test( value );
}, "A positive or negative non-decimal number please" ); $.validator.addMethod( "time", function( value, element ) {
return this.optional( element ) || /^([01]\d|2[0-3]|[0-9])(:[0-5]\d){1,2}$/.test( value );
}, "Please enter a valid time, between 00:00 and 23:59" ); // 自定义当前实例的规则 ??????
// 自定义全局规则
$.validator.config({
rules: {
integer: [/^-?\d+$/, "A positive or negative non-decimal number please"]
time: [/^([01]\d|2[0-3]|[0-9])(:[0-5]\d){1,2}$/, "Please enter a valid time, between 00:00 and 23:59"]
}
});
// 自定义当前实例的规则
$("#myform").validator({
rules: {
integer: [/^-?\d+$/, "A positive or negative non-decimal number please"]
time: [/^([01]\d|2[0-3]|[0-9])(:[0-5]\d){1,2}$/, "Please enter a valid time, between 00:00 and 23:59"]
}
});
5. 设置参数默认值
$.validator.setDefaults
VS $.validator.config
jquery-validation
$.validator.setDefaults({
debug: true
});
nice-validator
$.validator.config({
debug: true
});
6. 提示与隐藏消息
jquery-validation
// 提示错误消息
var validator = $( "#myshowErrors" ).validate();
validator.showErrors({
"firstname": "I know that your firstname is Pete, Pete!"
});
// 隐藏错误消息 ??????
nice-validator
// 批量提示错误消息
$("#myform").validator("showMsg", {
"firstname": "I know that your firstname is Pete, Pete!"
});
// 提示字段错误消息
$("#firstname").trigger("showmsg", ["error", "I know that your firstname is Pete, Pete!"]);
// 隐藏字段错误消息
$("#firstname").trigger("hidemsg");
// 隐藏表单全部消息
$("#myform").trigger("hidemsg");
7. 检查是否验证通过
jquery-validation
// 检查表单
nice-validator
var validator = $( "#myform" ).validate();
if (validator.form()) {
// do something
}
// 检查字段
var validator = $( "#myform" ).validate();
if (validator.element( "#myselect" )) {
// do something
}
// 检查表单
if ($("#myform").isValid()) {
// do something
}
// 检查字段
if ($("#myselect").isValid()) {
// do something
}
8. 验证完毕的回调
jquery-validation
$("#myform").validate({
submitHandler: function(form) {
$(form).ajaxSubmit();
},
invalidHandler: function(event, validator) {
// do something
}
});
nice-validator
$("#myform").validator({
valid: function(form) {
$(form).ajaxSubmit();
},
invalid: function(form, errors) {
// do something
}
});
9. 销毁表单验证
jquery-validation
var validator = $("#myform").validate();
validator.resetForm();
nice-validator
$("#myform").validator("destroy");
Nice Jquery Validator 【从 jQuery Validation 迁移】的更多相关文章
- jQuery validator plugin之Validator
Validator.destroy() Destroys this instance of validator freeing up resources and unregistering event ...
- jQuery学习之:Validation表单验证插件
http://polaris.blog.51cto.com/1146394/258781/ 最近由于公司决定使用AJAX + Struts2来重构项目,让我仔细研究一下这两个,然后集中给同事讲讲,让每 ...
- jQuery.validator 详解二
前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...
- jQuery.validator 详解
jQuery.validator 详解二 前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示 ...
- (转)jquery.validator规则
登录|注册 收藏成功 确定 收藏失败,请重新收藏 确定 标题 标题不能为空 网址 标签 摘要 公开 取消收藏 分享资讯 传PPT/文档 提问题 写博客 传资源 ...
- jquery.form.js+jquery.validation.js实现表单校验和提交
一.jquery引用 主要用到3个js: jquery.js jquery.form.js jquery.validation.js 另外,为了校验结果提示本地化,还需要引入jquery.vali ...
- jQuery validator plugin之概要
jQuery validator 主页 github地址 demo学习 效果: Validate forms like you've never validated before! 自定义Valida ...
- jQuery validator plugin 之 custom methods 案例1:multi email
1.add method jQuery.validator.addMethod( "multiemail", function (value, element) { var ema ...
- jquery validator
jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目中早已用过,但看到这篇文章写得还是不错的,转载下与大家共同分享. 一.用前必备 官方网站: ...
随机推荐
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- vue项目报错Missing space before function parentheses的问题
问题描述为——函数括号前缺少空格 导致原因主要是,使用eslint时,严格模式下,会报错Missing space before function parentheses的问题,意思是在方法名和刮号之 ...
- SQL——MySQL数据类型
Text类型: Number类型: Date类型:
- overflow:hidden的清除浮动效果
我们都知道"overflow:hidden"可以溢出隐藏,即当内容元素的高度大于其包含块的高度时,设置该属性即可把内容区域超出来的部分隐藏,使内容区域完全包含在该包含块中. 然而& ...
- Linux下VCS2014和Verdi2015的联合使用
VCS和Verdi是IC设计中常用的两款开发工具.VCS是Synopsys公司的产品,和大家所熟知的ModeSim一样的都是EDA仿真工具.Verdi是Nocas公司(已经被Synopsys公司收购) ...
- CSS 伪选择器 focus-within 介绍
CSS中的 :focus-within 伪选择器可有点"不寻常",尽管它的名称看上去很简单,而且相当直观.但它的解释是:选择一个包含具有:focus 的任何子元素的元素.有点绕是不 ...
- Ubuntu虚拟机的安装
1.在VMware中新建虚拟机 注意W10的现在强制升级VMware所以大家,安装低版本的时候提示,升级就区官网下载一个新版本的即可. 这边大家填写自己的就好,要记住自己填写的! 这边我们等一会即可. ...
- json数据写入hbase
package main.scala.com.web.zhangyong168.cn.spark.java; import org.apache.hadoop.hbase.HBaseConfigura ...
- eatwhatApp开发实战(三)
在实战二中我们在eatwhatApp上增加了“添加店铺的功能”.接下来,我们来将添加的店铺显示出来,这里我们用到控件--ListView. 先上演示图: 首先,我们先设置布局: <Relativ ...
- NodeJS——在Sublime中配置NodeJS执行环境
这种方式比在DOS窗中直接执行更加高效!!! nodejs 1.运行Sublime,菜单上找到Tools ---> Build System ---> new Build System 2 ...