jQuery validate基本原则
Markup recommendations
Each input has a label associated with it: The for-attribute of the label refers to the id-attribute of the input.
|
1
2
|
<label for="firstname">Firstname</label><input id="firstname" name="fname"> |
The name attribute is '''required''' for input elements, the validation plugin doesn't work without it. Usually name and id attributes should have the same value.
Methods
A validation method implements the logic to validate any element. Provided are a set of default validation methods, such as required. Apart from required itself and equalTo, all validation methods declare an element valid when it has no value at all. That way an email field is optional unless required is specified. You can specify an element input to contain a valid email address, or nothing at all. Use jQuery.validator.addMethod to implement custom methods.
Rules
A validation rule applies one or more validation methods to an input element. You can specify validation rules via metadata or via plugin settings (option rules). The decision is often influenced by serverside infrastructure. If a web framework is used, it is often easier to use metadata, which is also good for fast prototyping. Plugin settings produce cleaner markup, though valid markup results from both.
Error message display
Error messages are handled via label elements with an additional class (option errorClass). The link between the message and the invalid element is provided via the labels for attribute. When provided in the markup, they are shown and hidden accordingly, and otherwise created on demand. By default, labels are created after the invalid element, this is also customizable (option errorPlacement). It is also possible to put them into an error container (option errorLabelContainer). To use a different element then a label, specify the errorElement option.
Example:
$(document).ready(function () {
var password_info = "password should be char (both upper and low), number and symbol from \' ~ ! @ # $ , % ^ & * ( . ) ' _ + | ? < > : \"; `\', length between 8~16";
jQuery.validator.addMethod('regexpassword', function(value, element){
var password_re1 = /[a-z]+/;
var password_re2 = /[A-Z]+/;
var password_re3 = /[0-9]+/;
var password_re4 = /[~!@#$,%^&*(.)'_+|?<>:";`]+/;
function passwdRegexpCheck(passwd){
if(passwd != null && passwd != ""){
var len = passwd.length;
if(len < 8 || len > 15){
return false;
}
}
var rg1 = password_re1.test(passwd);
if (rg1 == true){
var rg2 = password_re2.test(passwd);
if(rg2 == true){
var rg3 = password_re3.test(passwd);
if(rg3 == true){
var rg4 = password_re4.test(passwd);
return rg4;
}
return false;
}
return false;
}
return false;
};
return this.optional(element) || passwdRegexpCheck(value);
}, password_info);
$('#password_reset_form').validate({
messages : {
password: {
required: "please input a valid password",
minlength: "the min length is 8",
maxlength: "the max length is 16"
},
password_confirm: {
required: "please input a valid password again",
equalTo: "the twice inputed passwords should be the same."
}
},
rules : {
password: {
required: true,
regexpassword : true
},
password_confirm: {
required: true,
rangelength: [8, 16],
equalTo: "#password"
}
},
highlight: function (element) {
$(element).removeClass('validate valid');
$(element).addClass('validate invalid');
},
unhighlight: function (element) {
$(element).removeClass('validate invalid');
$(element).addClass('validate valid');
},
errorElement: 'span',
onfocusout: function (element, event) {
this.element(element);
}
});
});
Reference Link:
http://jqueryvalidation.org/reference/
jQuery validate基本原则的更多相关文章
- jQuery Validate 表单验证 — 用户注册简单应用
相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...
- jquery validate表单验证插件-推荐
1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...
- 修改 jquery.validate.js 支持非form标签
尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...
- 表单验证插件之jquery.validate.js
提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...
- Jquery客户端校验——jquery.validate.js
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...
- jquery.validate不用submit而用js提交的例子
$("#form").validate(); $("#btn).click(function(){ if($("#form").valid()){ $ ...
- ASP.NET MVC 5 Jquery Validate
ClientValidationEnabled 在asp.net mvc 5中ClientValidationEnabled默认为TRUE,所以也不需要刻意去设置 应用ValidationAttrib ...
- jQuery Validate验证框架详解
转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...
- JS验证控件jQuery Validate
jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...
随机推荐
- M2: XAML Controls
本小节介绍如何在界面上添加简单的XAML Controls, 本例中我们用到了Grid, TextBlock, Button, 和StackPanel控件.XAML自身所有的控件都声明在Windows ...
- Push推送原理
Push 的工作机制 APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器. 推送可以分为三个阶段. 第一阶段:.net应用程 ...
- 收藏:关于UseSubmitBehavior和OnClientClick同时使用,导致无法触发后台事件的问题
经常会有正样的需求,在用户做一个不易恢复并且容易误操作的动作时需要给用户以提示,用户确认后继续执行动作. 简单的解决方案是:前台用OnClientClick事件中使用Confirm给用弹出确认提示框, ...
- CentOS7.0关于libguestfs的bug
libguestfs,libguestfs-tools是用来在不启动虚拟机的情况下,快速简单访问虚拟机磁盘的工具. 今天在CentOS7.0系统上通过guestmount命令去mount虚拟机磁盘的时 ...
- hadoop常用基础命令
1.日志查询: 用户可使用以下命令在指定路径下查看历史日志汇总$ bin/hadoop job -history output-dir 这条命令会显示作业的细节信息,失败和终止的任务细节. 关于作业的 ...
- (实用篇)PHP实现队列及队列原理
队列是一种线性表,按照先进先出的原则进行的: PHP实现队列:第一个元素作为队头,最后一个元素作为队尾 <?php /** * 队列就是这么简单 * * @link */ $array = ar ...
- UITextView 点return 隐藏键盘
iOS开发中,发现UITextView没有想UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView return键隐藏键盘,可以通过判断输入的字 ...
- ZOJ 1061 Web Navigation
原题链接 题目大意:模拟一个浏览器,打开一个网页.后退或者前进,输出网址. 解法:用两个堆栈分别表示后退保存的网页和前进保存的网页.初始化时把当前页面压入后退堆栈的栈顶.要注意几点,一个是每次记得要清 ...
- SpringSecurity相关配置【SpringSecurityConfig】
SpringSecurity的配置相对来说有些复杂,如果是完整的bean配置,则需要配置大量的bean,所以xml配置时使用了命名空间来简化配置,同样,spring为我们提供了一个抽象类WebSecu ...
- ListView优化相关
链接1 http://www.jb51.net/article/35273.htm 链接2 http://www.cnblogs.com/xilinch/archive/2012/11/08/2760 ...