表单验证之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 ...
随机推荐
- Prometheus监控系统之入门篇(一)续
在上篇Prometheus监控系统之入门篇(一)中我们讲解了Prometheus的基本架构和工作流程, 并从0到1搭建了Prometheus服务,pushgateway以及告警系统. 本篇我们主要介绍 ...
- 解决IntelliJ IDEA Community 社区版 启动Tomcat插件 "Smart Tomcat" NullPointerException 空指针异常
IntelliJ IDEA Community社区版默认是没有Ultimate版的Tomcat Server,这时候就可以使用插件"Smart Tomcat"; 在"Ru ...
- 利用docker安装gitlab
安装docker 安装 virtualbox 下载 dockertoolbox并安装 官网的服务器一直连不上, 幸亏还有这个 https://get.daocloud.io/toolbox/ 比 ht ...
- linux 线程间发送信号
线程间通过 pthread_kill(thid,signo)给指定的thid线程发送signo信号. 创建线程与线程屏蔽字顺序 1. pthread_create(); pthread_sigm ...
- Caffe Ubuntu14.04 + CUDA 8 (支持GTX1080 1070等Pascal架构显卡)
1. 前言 本教程使用的系统是Ubuntu 14.04 LTS 64-bit,使用的CUDA版本为8. 理论上本教程支持Pascal架构显卡,如游戏卡GeForce GTX1070,GTX 1080, ...
- 吴裕雄--天生自然C语言开发:函数指针
#include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int main(void) { /* p 是函 ...
- Yii框架的学习指南(策码秀才篇)1-2 一步步学习yii framework
我也是新手,不过之前学习了tp和ci框架,所以尝鲜想看看yii到底有多强大. 如何新建一个webapp(网站应用)呢,下面是2个步骤: 1. cmd 切换到htdocs下面的yii目录下的framew ...
- 77)PHP,将session数据写到不用的存储介质中
首先我的siession数据可以写到:文件中 session数据区 或者数据库中, 那么怎么将文件中的session数据或者session数据区的数据写到 数据库中,,又或者任意挑选两个不同 ...
- 专利|Pct||
专利:有些专利写的尽量模糊,为了不让别人检出,让别人能轻易侵犯专利权 优先权:在本国申请后,在他国也是同一个专利人申请,并也是同一个申请日. 发明20年:实用新型外观设计:20年 Pct:专利合作条约 ...
- springboot学习笔记:3.配置文件使用概要
Spring Boot允许外化(externalize)你的配置,这样你能够在不同的环境下使用相同的代码. 你可以使用properties文件,YAML文件,环境变量和命令行参数来外化配置.使用@Va ...