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基本原则的更多相关文章

  1. jQuery Validate 表单验证 — 用户注册简单应用

    相信很多coder在表单验证这块都是自己写验证规则的,今天我们用jQuery Validate这款前端验证利器来写一个简单的应用. 可以先把我写的这个小demo运行试下,先睹为快.猛戳链接--> ...

  2. jquery validate表单验证插件-推荐

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.     1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素  3.鼠标离开后的正确.错误提示及鼠标移入时的帮 ...

  3. 修改 jquery.validate.js 支持非form标签

    尝试使用markdown来写一篇blog,啦啦啦 源代码传送门:github 在特殊情况下我们使用jquery.validate.js对用户输入的内容做验证的时候,表单并不是一定包含在form之中,有 ...

  4. 表单验证插件之jquery.validate.js

    提到表单验证的插件,第一个想到的就是jquery.validate.js,所以小生想在这里稍微详细地说一下这款插件的具体使用方法,便于理解,我直接附上整段demo的代码(没怎么调样式,主要是看js): ...

  5. Jquery客户端校验——jquery.validate.js

    jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...

  6. jquery.validate不用submit而用js提交的例子

    $("#form").validate(); $("#btn).click(function(){ if($("#form").valid()){ $ ...

  7. ASP.NET MVC 5 Jquery Validate

    ClientValidationEnabled 在asp.net mvc 5中ClientValidationEnabled默认为TRUE,所以也不需要刻意去设置 应用ValidationAttrib ...

  8. jQuery Validate验证框架详解

    转自:http://www.cnblogs.com/linjiqin/p/3431835.html jQuery校验官网地址:http://bassistance.de/jquery-plugins/ ...

  9. JS验证控件jQuery Validate

    jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证 ...

随机推荐

  1. 配置集成环境xampp-win32-1.7.0-installer.exe

    第一步:准备当然要有xampp-win32-1.7.0-installer.exe这个软件了啊,大家可以去网站上下载这个就不必多说了吧在安装之前一定要将apache,mysql,httpd这一系列进程 ...

  2. oracle数据库什么情况下创建索引比较好

    索引就好象一本字典的目录.凭借字典的目录,我们可以非常迅速的找到我们所需要的条目.数据库也是如此.凭借Oracle数据库的索引,相关语句可以迅速的定位记录的位置,而不必去定位整个表. 虽 然说,在表中 ...

  3. rnn lstm

    资料收集:https://github.com/kjw0612/awesome-rnn 代码+例子+物理意义:https://iamtrask.github.io/2015/11/15/anyone- ...

  4. Day08_面向对象第三天

      1.代码块(掌握) 1.概述     由{}扩起来的代码称之为代码块,类或者方法也可认为是代码块,但是一般不这么说,我们平时所说的代码块指的是孤零零的{} 2.代码块作用     局部代码块作用  ...

  5. QEMU Guest Agent

    QEMU Guest Agent It is a daemon program running inside the domain which is supposed to help manageme ...

  6. nohup启动命令(转)

    简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后台运行,如果启动该程序的控制台logout,则该进程随即终止. 要实现守护进程,一种方法是按守护进程的规则去编程(本站 ...

  7. [转载]在Android C/C++层添加LOG调试

    原文地址:C/C++层添加LOG调试">在Android C/C++层添加LOG调试作者:谢轩昂 在Android C/C++层添加LOG调试,并且在Logcat中输出的方法 第一步: ...

  8. Educational Codeforces Round 15 B

    B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  9. C++ Primer : 第九章 : vector变长、string的其他操作以及容器适配器

    vector变长机制.string的其他构造方法,添加.替换和搜索操作,string比较和数值转换,最后是容器适配器. vector对象是如何增长的 vector和string类型提供了一些成员函数, ...

  10. Apache安全配置

    vi /etc/httpd/conf/httpd.conf #编辑文件 ServerTokens OS  在44行修改为:ServerTokens Prod (在出现错误页的时候不显示服务器操作系统的 ...