表单验证之JQuery Validate控件
概述
jQuery Validation Plugin v1.14.0,基于JQuery,官网http://jqueryvalidation.org/
该插件捆绑了一套有用的验证方法,包括 URL 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 API。所有的捆绑方法默认使用英语作为错误提示信息,且已翻译成其他 37 种语言,调用插件包就可以切换为中文等语言。
其他控件parsley.js Version 2.3.9,该插件是基于JavaScript语言的,官网http://parsleyjs.org,在此不做研究。
引用插件
<script src="${base!}/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
<script src="${base!}/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
<script src="${base!}/assets/global/plugins/jquery-validation/js/localization/messages_zh.min.js" type="text/javascript"></script>
jquery.validate.min.js中包括插件基本验证规则;
additional-methods.min.js这个文件中有扩展的验证规则,以及添加新的验证规则需要写在该文件中;
messages_zh.min.js是提示信息汉字包;
添加自定义验证规则
在validate-methods.js中使用addMethod(name,method,message)方法;其中,参数 name 是添加的方法的名字。参数 method 是一个函数,接收三个参数 (value,element,param) 。value 是元素的值,element 是元素本身,param 是参数。Message是提示信息的设定。下面是添加了一个验证中文的方法,这样就可以在rules(js代码中)中调用
$.validator.addMethod("zhongwen", function(value, element) {
var hz = /^[\u4e00-\u9fa5]+$/;
return this.optional(element) || hz.test(value);
}, "请填写中文字符");
代码示例
部分验证框,样式设置在公用css中
<div class="form-group margin-top-20">
<label class="control-label col-md-3">姓名
<span class="required"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="text" class="form-control" name="name" /> </div>
</div>
</div>
<div class="form-group margin-top-20">
<label class="control-label col-md-3">昵称
<span class="required"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="text" class="form-control" name="nickname" /> </div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">邮箱
<span class="required"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="text" class="form-control" name="email" /> </div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">手机
<span class="required"> * </span>
</label>
<div class="col-md-4">
<div class="input-icon right">
<i class="fa"></i>
<input type="text" class="form-control" name="mobile" /> </div>
<span class="help-block"></span>
</div>
</div>
验证JS
function JqValidate()
{
return $( '#formLogin' ).validate({ errorElement: 'span', //default input error message container
errorClass: 'help-block help-block-error', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "", // validate all fields including form hidden input
rules: {//校验规则
name: {
required: true,
zhongwen:true,
minlength:2,
maxlength:15,
},
nickname: {
required: true,
NickName:true
},
email: {
required: true,
email: true
},
mobile:{
required: true,
isMobile:true,
},
phone:{
required: true,
isPhone:true,
},
IDcard:{
required: true,
isIDCard:true,
}, creditcard: {
required: true,
creditcard: true
},
},
messages: { //自定义提示信息
name: {
required: "请输入姓名",
minlength: "姓名至少由两个汉字组成",
maxlength: "姓名不超过15个汉字"
},
email: "请输入一个正确的邮箱",
gender:"必须选择一个性别属性",
agree:"请接受我方条款",
},
errorPlacement: function (error, element) { // 错误信息显示位置
var icon = $(element).parent('.input-icon').children('i');
icon.removeClass('fa-check').addClass("fa-warning");
icon.attr("data-original-title", error.text()).tooltip({'container': 'body'});
}, highlight: function (element) { // 高亮显示错误(错误变红)
$(element)
.closest('.form-group').removeClass("has-success").addClass('has-error'); // set error class to the control group
}, unhighlight: function (element) { // revert the change done by hightlight }, success: function (label, element) {//数据正确输入后由红色编程绿色
var icon = $(element).parent('.input-icon').children('i');
$(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
icon.removeClass("fa-warning").addClass("fa-check");
}, submitHandler: function (form) {
alert("数据校验正确!");
//form[0].submit(); // submit the form
}
});
}
效果

参考网站
菜鸟教程的JQuery validate
表单验证之JQuery Validate控件的更多相关文章
- 表单验证插件jquery.validate的使用方法演示
jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...
- jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js
原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352 最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...
- jq中的表单验证插件------jquery.validate
今天我们来说一下表单验证,有人说我们在进行表单验证的时候使用正则来验证是非常麻烦的,现在我来给大家介绍一下表单验证的插件:jquery.validate.min.js 它是与jquery一起结合用来使 ...
- jQuery插件 -- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- jQuery表单验证插件——jquery.validate.js
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script src="../j ...
- 表单验证神器——jquery.validate插件
jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- jQuery框架学习第十一天:实战jQuery表单验证及jQuery自动完成提示插件
jQuery框架学习第一天:开始认识jQueryjQuery框架学习第二天:jQuery中万能的选择器jQuery框架学习第三天:如何管理jQuery包装集 jQuery框架学习第四天:使用jQuer ...
- jQuery 表单验证插件 jQuery Validation Engine 使用
jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...
随机推荐
- 一.Memcached企业服务
1.Memcached是什么,有什么作用? Memcached是一个开源的.高性能的内存缓存软件,从名称上看Mem就是内存的意思,而Cache就是缓存的意思. ...
- Codeforces 1288B - Yet Another Meme Problem
题目大意: 令conc(a,b)函数得出的结果为将ab拼接得到的数字. 例如:conc(12,23)=1223 a和b不会包括前导0! 接下来,你已知A和B,问有多少对的(a,b)满足 1≤a≤A , ...
- Codeforces Round #556(Div.1)
A 容易发现i,i+1至少有一个数出现,于是可以让尽量多的2和奇数出现 #include<bits/stdc++.h> using namespace std; int n,s1,s2; ...
- Perl:理解正则中“.”可匹配出回车符(“\n”)外任意字符的例子,配合 $^I 关键字
要把下面文件的内容改了, Program name: graniteAuthor: Gilbert BatesCompany: RockSoftDepartment: R&DPhone: +1 ...
- 如何用java实现图片与base64转换
如果你是一个软件开发,不论前端后端工程师,图片的处理你是肯定要会的,关于图片的Base64编码,你可能有点陌生,但是这是一个软件工程师应该要掌握的知识点,现在很多网友把图片与base64转换都做成了小 ...
- js 实现时间递增,当前时间功能,javascript格式化当天日期
直接上代码: function nowTimeStr() { var date = new Date(); ...
- NOIP2002 过河卒(DFS,DP)
https://www.luogu.org/problem/P1002 题目描述 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同时在棋盘上的任一点有一个对方的马(如 ...
- 使用mui框架开发App,当input获取焦点时,键盘弹出,底部导航栏上升。
转自 https://blog.csdn.net/elementFei/article/details/72917393 感谢 问题: 使用mui框架开发App,当input获取焦点时,键盘弹出,底部 ...
- mysql关键字汇总
ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGINT BINARY BLOB BOTH BY CALL CASCADE C ...
- PAT甲级——1006 Sign In and Sign Out
PATA1006 Sign In and Sign Out At the beginning of every day, the first person who signs in the compu ...