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 和电子邮件验证 ...
随机推荐
- magneto创建运费模板
Magento系统自带了大概7种运费方式:平价.运费表.免运费.ups.usps.fedex.dhl等.不过这些依然无法满足我们的需求,这时候就需要创建一个shipping module 来实现了.创 ...
- matlab的调试方法
• 常用的调试方法.• (1) 设置或清除断点:使用快捷键F12. • (2) 执行:使用快捷键F5.• (3) 单步执行:使用快捷键F10. • (4) step in:当遇见函数时,进入函数内部, ...
- which和whereis 命令
which 文件名 whereis 程序名
- Nexus仓库构建
1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...
- win7录屏工具
psr.exe http://jingyan.baidu.com/article/aa6a2c14d330710d4d19c47c.html
- Generic Netlink详解
netlink socket是一种用于用户态进程和内核态进程之间的通信机制.它通过为内核模块提供一组特殊的API,并为用户程序提供了一组标准的socket接口的方式,实现了全双工的通讯连接. Netl ...
- 三 JSP 技术
一 JSP 概述 1. 本质:在 HTML 语言中混合 Java 程序代码,由服务器端 Java 语言引擎解释执行.其中,HTML 负责描述信息显示格式,JSP 负责描述处理逻辑. 2. JSP 代码 ...
- 关于typedef的用法总结(转)
不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...
- spring学习笔记---第三方SDK(Rest API)和Jaskson的巧用
前言: 其实我以前一直不懂电商, 以及电商中所涉及的业务概念. 对于SKU等名词, 觉得有些玄乎. 对其背后的数据模型, 也有莫名的未知恐惧感: 庞大而理不清头绪. 不过最近有机会接触了微商(有赞), ...
- JavaScript BOM 遗漏知识再整理;弹窗和记时事件;
1.JavaScript 弹窗 警告框 警告框经常用于确保用户可以得到某些信息. 当警告框出现后,用户需要点击确定按钮才能继续进行操作. window.alert() 方法可以不带上window对象, ...