转载 jQuery validation
之前做客户端验证感觉自己javascript 不行,虽然能写出来一完整的验证,但从不自信,一直觉得客户端验证是比较繁琐的事情,但是又不能不做,只到最开始接触ajax ,遇到了一个jQuery validation 的插件,在网上看功能比较强大,而且看起来比较容易,所以就尝试一下,效果还真不错!
jQuery validation 插件提供的一些DEMO.在这些DEMO 都是一些比较常用而且很好的例子:中间
$.validator.setDefaults({
debug: true
});
设置默认的状态为debug,这样用户的数据就不会提交了。
接下来是一段比较长的代码,但是不用怕,因为真的挺简单的//当页面载入完成时,执行以下动作
$().ready(function() {
//验证指定表单
$("#myform").validate({
//设置默认的状态为keyup,也可以设置为blur
event: "keyup", //触发验证的事件
//设定规则
rules: {
//对应id为'username'的input
username: {
//必填项
required: true,
//最多和最少的字符数
rangeLength:[4,16]
},
mail: {
required: true,
//声明这是一个电子邮件
email: true
},
password: {
required: true,
//最少4个字符
minLength: 4
},
confirm_password: {
required: true,
minLength: 4,
// 与哪个等同,这里是id为password的input等同
equalTo: "#password"
},
agree: "required"
},
//这里是与规则对应的错误代码
messages: {
username: {
//如果用户名为空,则显示下面的信息
required: '请输入用户名',
//如果字符串的长度不符合,则显示下面的信息
rangeLength: '用户名必须在4-16个字符之间'
},
password: {
required: '请输入密码',
minLength: '密码必须大于4个字符'
},
confirm_password: {
required: '请确认你的密码',
equalTo: '两次密码输入不一致',
minLength: '密码必须大于4个字符'
},
agree: '请同意我们的条款',
mail: '请输入有效的E-MAIL帐户'
}
});
});
下面是一个比较简单的例子:
HTML 代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>jQuery验证</title>
<link rel="stylesheet" href="theme.css" type="text/css"/>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="fv.js"></script>
</head>
<body>
<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" />
</p>
<p>
<label for="lastname">Lastname</label>
<input id="lastname" name="lastname" />
</p>
<p>
<label for="username">Username</label>
<input id="username" name="username" />
</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" />
</p>
<p>
<label for="agree">Please agree to our policy</label>
<input type="checkbox" id="agree" name="agree" />
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
</body>
</html>
JS 代码也不是jquery validation 验证代码:
//验证通过表单提交的方式
$.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: "Please enter your firstname",
lastname: "Please enter your lastname",
username: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
confirm_password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
},
email: "Please enter a valid email address",
agree: "Please accept our policy"
}
});
/**
验证确认密码通过后,更改密码时,验证确认密码的合法性
**/
$("#password").blur(function() {
$("#confirm_password").valid();
});
success: function(label) {
label.html('√').addClass("success");
}
});
另外就是css 样式文件:
#signupForm { width: 670px;}
#signupForm label.error {
margin-left: 10px;
width: auto;
display: inline;
}
#newsletter_topics label.error {
display: none;
margin-left: 103px;
}
body{
font-size:16px;
color:blue;
}
form.cmxform label.error, label.error {
/* remove the next line when you have trouble in IE6 with labels in list */
color: red;
font-style: normal
}
div.error { display: none; }
input { border: 1px solid black; }
input:focus { border: 1px dotted black; }
input.error { border: 1px dotted red; }
运行上面的程序需要jQuery.js jQuery 类库文件,以及 jQuery validation 插件的库文件 jquery.validation.js 文件.
转载 jQuery validation的更多相关文章
- jquery.validation.js 表单验证
jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuer ...
- Jquery Validation 验证控件的使用说明
转载自:http://blog.csdn.net/huang100qi/article/details/52453970,做了一些简化及修改 下载地址:https://jqueryvalidation ...
- jQuery Validation Engine 表单验证
功能强大的 jQuery 表单验证插件,适用于日常的 E-mail.电话号码.网址等验证及 Ajax 验证,除自身拥有丰富的验证规则外,还可以添加自定义的验证规则. 兼容 IE 6+, Chrome, ...
- 表单验证插件----jquery validation
1.下载地址:http://jqueryvalidation.org/ 2.使用方法: <script type="text/javascript" src="ht ...
- jquery and jquery validation 常见问题解决
Cannot read property 'settings' of undefined jquery validation 这个问题常常发生在动态添加rule的时候. 解决方法 // 在timeou ...
- 如何解决jQuery Validation针对动态添加的表单无法工作的问题?
为了充分利用ASP.NET MVC在服务端呈现HTML的能力,在<利用动态注入HTML的方式来设计复杂页面>一文中介绍了,通过Ajax调用获取HTML来呈现复杂页面中某一部分界面的解决方案 ...
- jQuery Validation remote的缓存请求
不知大家有没有遇到,用jQuery Validation(本文讨论的版本为jQuery Validation Plugin 1.11.1)用remote方式做校验时,如果验证元素的值保持一致,进行多次 ...
- jQuery 表单验证插件 jQuery Validation Engine 使用
jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...
- jQuery Validation Plugin学习
http://blog.csdn.net/violet_day/article/details/14109261 jQuery Validation Plugin Demo 一.默认校验规则 (1)r ...
随机推荐
- [Effective Java]第十章 并发
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 关于JAVA中URL传递中文参数,取值是乱码的解决办法
前几天看到有网友在问URLDecoder和URLEncoder方面的使用问题,突然想起,原来我刚遇到这两个类时,也觉得很神密,由此可以想想初学者的心情,于是便有了今天的这篇文章. 其实,这两个类的使用 ...
- spring常用的工具类
spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...
- 学习笔记day6:CSS3动画属性
总结: 1: CSS动画:@keyframes animation:ie10+:加-webkit前缀: animation 则是属于关键帧动画的范畴; 它本身被用来替代一些纯粹表现的javascri ...
- poj1819Disks
链接 题意:从左到右按顺序给你n个圆的半径,把左右两边想象成两堵墙的话,就是左右两边向里挤压,问哪些圆是对最后的宽度不影响. 刚开始理解错了,..以为怎么放圆使宽度最小.. 这样就可以尽量使每个圆向左 ...
- Oracle 中union的用法
UNION 指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果. 例如: SELECT Date FROM Store_Information UNION SELECT Date ...
- number-of-segments-in-a-string
https://leetcode.com/problems/number-of-segments-in-a-string/ package com.company; class Solution { ...
- Android 让输入框输入指定字符的办法
让输入框输入指定字符的办法 有一个需求 让输入密码的时候只能输入数字字母可见字符 不能输入中文 之前还以为要写代码 还来发现有一个属性可以直接实现 <EditText android:layou ...
- [CSS] vertical-align
原文地址: http://www.zhangxinxu.com/wordpress/2010/05/%E6%88%91%E5%AF%B9css-vertical-align%E7%9A%84%E4%B ...
- 常用的 文件 MIME类型
估计很多朋友对不同后缀的文件对应的MIME类型不熟悉(实际上这么多我也记不住), 所以将平常常见的一些文件后缀对应的MIME类型写了一个对照表,现在奉献给大家: .asx,video/x-ms-asf ...