表单验证之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 ...
随机推荐
- Android 自定义dialog类
首先定制style样式 styles.xml 加入自定义样式 <style name="CustomLoadingDialog"> <item name=&quo ...
- 视觉SLAM算法框架解析(1) PTAM
版权声明:本文为博主原创文章,未经博主允许不得转载. 本系列文章旨在总结主流视觉SLAM算法的框架,对比各个算法在子模块的差异,最终提炼出融合各个算法优点的架构. PTAM[1]是视觉SLAM领域里程 ...
- Linux centos 下安装redis
一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.选择安装文件 ...
- 计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)
题:https://codeforces.com/contest/1245/problem/F 分析:转化为:求区间内满足a&b==0的对数(解释见代码) ///求满足a&b==0在区 ...
- logstash nested内嵌字段 field protobuf解码 codec 的解决办法
logstash nested内嵌字段 field protobuf解码 codec 的解决办法 主要需求 logstash-codec 下https://www.elastic.co/guide/e ...
- rest framework-restful介绍-长期维护
############### django框架-rest framework ############### # django rest framework 框架 # 为什么学习这个res ...
- Simple Random Sampling|representative sample|probability sampling|simple random sampling with replacement| simple random sampling without replacement|Random-Number Tables
1.2 Simple Random Sampling Census, :全部信息 Sampling: 抽样方式: representative sample:有偏向,研究者选择自己觉得有代表性的sam ...
- gpio 的配置
1.时钟使能 B引脚RCC时钟脉冲启动.|(或)表示只要第三个为1就行. 也可以写为RCC_APB2ENR | = 0X0008; 2.配置GPIO的输出模式以及速度 3 . 如果是 0号引脚:GP ...
- ExecuteScalar()方法的使用
ExecuteScalar()方法的作用是: 执行查询,并返回查询所返回的结果集中第一行的第一列.所有其他的列和行将被忽略. 1.返回的是一个object类型,也就是说是所有数据类型的基类,可根据se ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...