jQuery Validate插件实现表单强大的验证功能
转自:http://www.jb51.net/article/76595.htm
jQuery Validate插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。
第一节:jQuery Validation让验证变得如此容易
一、官网下载jquery,和jquery validation plugin
二、引入文件
<script src=
"js/jquery-1.8.0.min.js"
type=
"text/javascript"
></script>
<script src=
"js/jquery.validate.js"
type=
"text/javascript"
></script>
<
form
action
=
""
id
=
"jvForm"
>
姓名:<
input
type
=
"text"
name
=
"username"
id
=
"username"
class
=
"required"
/></
br
>
密码:<
input
type
=
"password"
name
=
"password"
id
=
"password"
class
=
"required"
/></
br
>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
他的作用就是在这个inpute标签为空时会提示用户出错。
<script type=
"text/javascript"
>
$(
function
() {
$(
"#jvForm"
).validate();
})
</script>

第二节:jQuery Validation让验证变得如此容易
上一个例子我们是统一引用jquery.validate.js这样所有必填字段的提示信息都将是This field is required.
现在要改成动态提示,比如姓名如果为空则提示姓名不能为空,密码如果为空则提示密码不能为空。
这次我们将校验规则写在代码里
首先还是先引入文件
<script src=
"js/jquery-1.8.0.min.js"
type=
"text/javascript"
></script>
<script src=
"js/jquery.validate.js"
type=
"text/javascript"
></script>
<
form
action
=
""
id
=
"jvForm"
>
姓名:<
input
type
=
"text"
name
=
"username"
id
=
"username"
/></
br
>
密码:<
input
type
=
"password"
name
=
"password"
id
=
"password"
/></
br
>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
最后 校验规则如下:
$(
function
() {
$(
"#jvForm"
).validate({
rules: {
username: {
required:
true
},
password: {
required:
true
}
},
messages: {
username: {
required:
"姓名不能为空!"
},
password: {
required:
"密码不能为空!"
}
}
});
})

第三节:jQuery Validation让验证变得如此容易
以下代码进行对jQuery Validation的简单演示包括必填项、字符长度,格式验证
一、引入文件
<script src=
"js/jquery-1.8.0.min.js"
type=
"text/javascript"
></script>
<script src=
"js/jquery.validate.js"
type=
"text/javascript"
></script>
<
form
action
=
""
id
=
"jvForm"
>
用 户 名:<
input
type
=
"text"
name
=
"username"
/></
br
>
密 码:<
input
type
=
"password"
name
=
"password"
id
=
"password"
/></
br
>
确认密码:<
input
type
=
"password"
name
=
"confirm_password"
/></
br
>
出 生 地:<
select
name
=
"address"
><
option
value
=
""
>--</
option
><
option
value
=
"1"
>北京</
option
>
<
option
value
=
"1"
>上海</
option
><
option
value
=
"1"
>深圳</
option
></
select
></
br
>
手 机:<
input
type
=
"text"
name
=
"mobile"
/></
br
>
邮 箱:<
input
type
=
"text"
name
=
"email"
/></
br
>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
<style type=
"text/css"
>
label.error{
font-size
:
12px
;
font-weight
:
normal
;
color
:
#ff0511
;
margin-left
:
10px
;}
</style>
<script type =
"text/javascript"
>
$(
function
() {
$(
"#jvForm"
).validate({
rules: {
username: {
//用户名必填 至少3位
required:
true
,
minlength: 3
},
password: {
//密码必填 至少6位
required:
true
,
minlength: 6
},
confirm_password: {
//密码确认
required:
true
,
equalTo:
"#password"
},
address: {
//出生地必填
required:
true
},
mobile: {
//手机必填 验证格式
required:
true
,
mobile:
true
},
email: {
//email必填 验证格式
required:
true
,
email:
true
},
},
messages: {
username: {
required:
"用户名不能为空!"
,
minlength:
"用户名至少三位!"
},
password: {
required:
"密码不能为空!"
,
minlength:
"密码至少六位!"
},
confirm_password: {
required:
"密码确认不能为空!"
,
equalTo:
"两次输入密码不一致 !"
},
address: {
required:
"请选择出生地!"
,
},
mobile: {
required:
"手机不能为空!"
,
mobile:
"手机格式不正确"
,
},
email: {
required:
"邮箱不能为空!"
,
email:
"邮箱格式不正确"
,
},
}
});
})
</script>

jQuery Validate插件实现表单强大的验证功能的更多相关文章
- jQuery Validate 插件为表单提供了强大的验证功能
之前项目开发中,表单校验用的jQuery Validate 插件,这个插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的 ...
- validate插件实现表单效验(二)
一款优秀的表单验证插件——validation插件 特点: l 内置验证规则:拥有必填.数字.email.url和信用卡号码等19类内置验证规则 l 自定义验证规则:可以很方便的自定义验证规则 l ...
- jquery validate 一个注册表单的应用
先看页面 前端表单代码 register.html <form class="mui-input-group" id="regForm"> < ...
- 表单提交学习笔记(二)—使用jquery.validate.js进行表单验证
一.官网下载地址:http://plugins.jquery.com/validate/ 二.用法 1.在页面上进行引用 <script src="~/scripts/jquery-1 ...
- jquery.validate.js自定义表单验证
$(document).ready(function() { //在下列位置输入页面加载的逻辑代码 $("#inputForm").validate({ rules: { seq: ...
- validate插件实现表单效验(一)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [转]jQuery.validate插件在失去焦点时执行验证代码
转:http://my.oschina.net/enyo/blog/311566 关于 jquery.validate.js 表单验证插件如何在失去焦点时做验证.看手册后发现默认是在表单提交时执行验证 ...
- jQuery html5Validate基于HTML5表单 异步服务器端验证
1. HTML5 自带的Validate 很漂亮,很好用, 但是一定要在form里用submit按钮,才生效 <form id="frmInfo" action=" ...
- jquery.form插件 提交表单 type="hidden"取不到值的问题记录
1.外国文献:说可以改成其他的(非hidden),再加style="display:none"隐藏. <INPUT type="password" sty ...
随机推荐
- C#服务端通过Socket推送数据到Android端App中
需求: 描述:实时在客户端上获取到哪些款需要补货. 要求: 后台需要使用c#,并且哪些需要补货的逻辑写在公司框架内,客户端采用PDA(即Android客户端 版本4.4) . 用户打开了补货通知页面时 ...
- vue-cli中圣杯布局失效问题
众所周知vue2在前端框架越来越流行,vue-cli这个脚手架工具是我们前端开发必用的,大大的提升了我们的开发效率.当然对于前端小白来说,有些遇到的问题需要和大家分享一下. 移动端页面经常都是需要圣杯 ...
- [Algorithm] 11. Linked List Cycle
Description Given a linked list, determine if it has a cycle in it. To represent a cycle in the give ...
- Mybatis-generator插件
Mybatis-generator插件 1.下载地址 https://github.com/mybatis/generator/releases mybatis-generator是一款在使用myba ...
- Spring Boot之简单的MVC
最近开始看Spring Boot,发现其开发起来真是方便.今天就来实现一个简单的Spring MVC 请求,纯Java代码的哦. 1.Maven必不可少,先看看都加载了那些依赖: <?xml v ...
- NFV
转载: NFV介绍定义:NFV,即网络功能虚拟化,Network Function Virtualization.通过使用x86等通用性硬件以及虚拟化技术,来承载很多功能的软件处理.从而降低网络昂贵的 ...
- python socket实现文件传输(防粘包)
1.文件传输的要点: 采用iterator(迭代器对象)迭代读取,提高读取以及存取效率: 通过for line in file_handles逐行conn.send(): 2.socket粘包问题: ...
- Python之两个值对换
- Python基础之生成器、迭代器
一.字符串格式化进阶 Python的字符串格式化有两种方式: 百分号方式.format方式,由于百分号的方式相对来说比较老,在社区里讨论format方式有望取代百分号方式,下面我们分别介绍一下这两种方 ...
- 洛谷—— P3811 【模板】乘法逆元
https://www.luogu.org/problem/show?pid=3811 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式 ...