jQuery学习笔记(jquery.validate插件)
jquery.validate官网地址:http://jqueryvalidation.org/
1. 导入JavaScript库
<script src="../js/jquery.js"></script>
<script src="../js/jquery.validate.js"></script>
<script src="../js/messages_zh.js"></script>
jquery.js文件不用说了,jquery.validate.js文件为验证脚本程序,messages_zh.js文件为消息本地化代码。
2. 默认检验规则
required– Makes the element required.remote– Requests a resource to check the element for validity.minlength– Makes the element require a given minimum length.maxlength– Makes the element require a given maxmimum length.rangelength– Makes the element require a given value range.min– Makes the element require a given minimum.max– Makes the element require a given maximum.range– Makes the element require a given value range.email– Makes the element require a valid emailurl– Makes the element require a valid urldate– Makes the element require a date.dateISO– Makes the element require an ISO date.number– Makes the element require a decimal number.digits– Makes the element require digits only.creditcard– Makes the element require a credit card number.equalTo– Requires the element to be the same as another one
懒得翻译了,应该很好懂的含义吧。
3. 方式一,将检验规则写到控件中
<script src="../js/jquery.js"></script>
<script src="../js/jquery.validate.js"></script>
<script src="../js/messages_zh.js"></script> <script>
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
}); $().ready(function() {
// validate the comment form when it is submitted
$("#commentForm").validate();
});
</script> <form class="cmxform" id="commentForm" method="get" action="" style="width: 500px;">
<fieldset>
<legend>请输入信息...</legend>
<p>
<label for="cname">姓名</label>
<input id="cname" name="name" minlength="2" type="text" required />
<p>
<label for="cemail">E-Mail</label>
<input id="cemail" type="email" name="email" required />
</p>
<p>
<label for="curl">URL</label>
<input id="curl" type="url" name="url" />
</p>
<p>
<label for="ccomment">评价</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
请输入信息...
姓名
URL
评价
注意<input>元素中的:required/minlength="2"/type="email"等代码。正是它们决定了如何验证此元素。
备注:不知道为什么cnblog居然会对form代码进行检查,required/minlength属性居然加不进去,奇怪!
4. 方式二,在JavaScript脚本中写规则
<script src="../js/jquery.js"></script>
<script src="../js/jquery.validate.js"></script>
<script src="../js/messages_zh.js"></script> <script>
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
}); $().ready(function() {
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: "required",
lastname: "required",
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true
},
topic: {
required: "#newsletter:checked",
minlength: 2
},
agree: "required"
},
messages: {
firstname: "留下大名",
lastname: "请问你姓啥?",
username: {
required: "写个用户名哦",
minlength: "用户名最少2个字符"
},
password: {
required: "请输入密码",
minlength: "密码至少5个字符哦"
},
confirm_password: {
required: "请输入密码",
minlength: "密码至少5个字符哦",
equalTo: "两次输入的密码要一致哦"
},
email: "请输入一个有效的电子邮件地址",
agree: "请选择同意协议"
}
}); // propose username by combining first- and lastname
$("#username").focus(function() {
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
if(firstname && lastname && !this.value) {
this.value = firstname + "." + lastname;
}
}); //code to hide topic selection, disable for demo
var newsletter = $("#newsletter");
// newsletter topics are optional, hide at first
var inital = newsletter.is(":checked");
var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
var topicInputs = topics.find("input").attr("disabled", !inital);
// show when newsletter is checked
newsletter.click(function() {
topics[this.checked ? "removeClass" : "addClass"]("gray");
topicInputs.attr("disabled", !this.checked);
});
});
</script> <form class="cmxform" id="signupForm" method="get" action="">
<fieldset>
<legend>Validating a complete form</legend>
<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" type="text" />
</p>
<p>
<label for="lastname">Lastname</label>
<input id="lastname" name="lastname" type="text" />
</p>
<p>
<label for="username">Username</label>
<input id="username" name="username" type="text" />
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password" />
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="email" />
</p>
<p>
<label for="agree">Please agree to our policy</label>
<input type="checkbox" class="checkbox" id="agree" name="agree" />
</p>
<p>
<label for="newsletter">I'd like to receive the newsletter</label>
<input type="checkbox" class="checkbox" id="newsletter" name="newsletter" />
</p>
<fieldset id="newsletter_topics">
<legend>Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo</legend>
<label for="topic_marketflash">
<input type="checkbox" id="topic_marketflash" value="marketflash" name="topic" />
Marketflash
</label>
<label for="topic_fuzz">
<input type="checkbox" id="topic_fuzz" value="fuzz" name="topic" />
Latest fuzz
</label>
<label for="topic_digester">
<input type="checkbox" id="topic_digester" value="digester" name="topic" />
Mailing list digester
</label>
<label for="topic" class="error">Please select at least two topics you'd like to receive.</label>
</fieldset>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
Validating a complete form
Firstname
Lastname
Username
Password
Confirm password
Please agree to our policy
I'd like to receive the newsletter
Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo Marketflash Latest fuzz Mailing list digester Please select at least two topics you'd like to receive.
注意:
- 在rules标签下面的以下用法:required: true/agree: "required";email: true;minlength: 5等等。
- 在messages标签下面可以自定义对应于任一元素所对应的任意条件不满足时的提示消息,这点非常实用。
- 可以设置validate的默认值,写法如下(需要使用form.submit()而不要使用$(form).submit()):
$.validator.setDefaults({
submitHandler: function(form) { alert("submitted!"); form.submit(); }
});
5. 其它技巧
5.1 用其他方式替代默认的SUBMIT
$().ready(function() {
$("#signupForm").validate({
submitHandler:function(form){
alert("submitted");
form.submit();
}
});
});
5.2 只验证不提交表单
$().ready(function() {
$("#signupForm").validate({
debug:true
});
});
jQuery学习笔记(jquery.validate插件)的更多相关文章
- jquery学习笔记---jquery插件开发
http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html jquery插件开发:http://www.cnblogs.com/damonla ...
- JQuery学习笔记---jquery对象和DOM对象的关系
1.DOM(Document Object Model,文档对象模型).DOM树 { html (head&&body), head(meta && title) ...
- jquery学习笔记----jquery相关的文档
http://tool.oschina.net/apidocs/apidoc?api=jquery http://www.w3school.com.cn/jquery/jquery_ref_event ...
- JQuery学习笔记——JQuery基础
#,JQuery避免名称冲突的方法 var jq = jQuery.noConfilct(); jq.ready( function(){ jq("p").hidden() ...
- jQuery学习笔记——jQuery常规选择器
一.简单选择器在使用 jQuery 选择器时,我们首先必须使用“$()”函数来包装我们的 CSS 规则.而CSS 规则作为参数传递到 jQuery 对象内部后,再返回包含页面中对应元素的 jQuery ...
- jQuery 学习笔记(jQuery: The Return Flight)
第一课. ajax:$.ajax(url[, settings]) 练习代码: $(document).ready(function() { $("#tour").on(" ...
- jQuery学习笔记——jQuery基础核心
代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起始的.而这个“$”就是jQuery当中最重要且独有的对象:jQuery对象,所以我们在页面元素选择或执行功能 ...
- jQuery学习笔记之插件开发(4)
jQuery学习笔记之插件开发(4) github源码地址 插件:了让原有功能的增强. 1.插件的种类(3种):局部.全局.选择器插件 1.1封装对象方法的插件 这种类型的插件是把一些常用或者重复使用 ...
- jQuery 学习笔记
jQuery 学习笔记 一.jQuery概述 宗旨: Write Less, Do More. 基础知识: 1.符号$代替document.getElementById( ...
- jQuery学习笔记 - 基础知识扫盲入门篇
jQuery学习笔记 - 基础知识扫盲入门篇 2013-06-16 18:42 by 全新时代, 11 阅读, 0 评论, 收藏, 编辑 1.为什么要使用jQuery? 提供了强大的功能函数解决浏览器 ...
随机推荐
- 【MongoDB:】稍微复杂的操作
1:插入数据稍微复杂的形式 doc=( {"user_id" : "ABCDBWN", "password" :"ABCDBWN& ...
- Hibernate学习笔记一:项目创建与基本配置文件
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6760773.html 一:ORM ORM:对象-关系 映射. 即:把Java中有关联关系的对象,转换成关系型 ...
- 〖Linux〗在tmux同时使用bash和zsh
个人有两份tmux配置文件: ~/.tmux.conf # 使用zsh,主要是日常使用,zsh太好使用了 ~/.tmux.conf.bash # 使用bash,主要是Android编译使用 按照tmu ...
- SpringCloud stream连接RabbitMQ收发信息
百度上查的大部分都是一些很简单的单消费者或者单生产者的例子,并且多是同一个服务器的配置,本文的例子为多服务器配置下的消费生产和消费者配置. 参考资料:https://docs.spring.io/sp ...
- python之模块配置文件ConfigParser(在python3中变化较大)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块ConfigParser(在python3中为configparser) #特别注意:py ...
- 利用Git进行团队协作
前言: 这里简单介绍一下Git的历史. 同生活中的许多伟大事件一样,Git 诞生于一个极富纷争大举创新的年代.Linux 内核开源项目有着为数众广的参与者.绝大多数的 Linux 内核维护工作都花在了 ...
- C++基础学习教程(六)----类编写的前情回想以及项目实战(1)
在開始类的编写之前我们依旧须要回想整理一下前面所说的内容,(前面尽管是一个自己定义数据类型的实现过程,可是内容有点繁杂). 先看一段代码: /** @file calssStruct.cpp */ / ...
- selectAll, unSelectAll两个操作的实现
private void updateBatchSelectionStatus() { ContactListAdapter.ViewHolder viewHolder = null; ...
- httpclient获取响应实体和信息的封装方法(解耦更新)
转自:https://blog.csdn.net/fhaohaizi/article/details/77850302 2018年07月19日更新,主要是解耦之后方法很多地方发生了变化,httpcli ...
- springmvc概述及框架原理
一. 前言 MVC不是框架而是一种设计模式. MVC的全名Model View Controller,即模型-视图-控制器的缩写,这是一种设计模式,而非架构.MVC它强制的使用应用程序的输入.处理.和 ...