概述

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控件的更多相关文章

  1. 表单验证插件jquery.validate的使用方法演示

    jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...

  2. jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js

    原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352   最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...

  3. jq中的表单验证插件------jquery.validate

    今天我们来说一下表单验证,有人说我们在进行表单验证的时候使用正则来验证是非常麻烦的,现在我来给大家介绍一下表单验证的插件:jquery.validate.min.js 它是与jquery一起结合用来使 ...

  4. jQuery插件 -- 表单验证插件jquery.validate.js

    最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...

  5. 表单验证插件jquery.validate.js

    最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...

  6. jQuery表单验证插件——jquery.validate.js

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script src="../j ...

  7. 表单验证神器——jquery.validate插件

    jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  8. jQuery框架学习第十一天:实战jQuery表单验证及jQuery自动完成提示插件

    jQuery框架学习第一天:开始认识jQueryjQuery框架学习第二天:jQuery中万能的选择器jQuery框架学习第三天:如何管理jQuery包装集 jQuery框架学习第四天:使用jQuer ...

  9. jQuery 表单验证插件 jQuery Validation Engine 使用

    jQuery 表单验证插件 jQuery Validation Engine 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面),指定使用 jQuery Validation Engi ...

随机推荐

  1. mysql Communications link failure Last packet sent to the server was X ms ago

    想必大家在用MySQL时都会遇到连接超时的问题,如下图所示: 就是这个异常(com.mysql.jdbc.exceptions.jdbc4.Communication***ception:Commun ...

  2. Spring Boot原理

    Spring 钩子之BeanFactoryPostProcessor和BeanPostProcessor的源码学习 https://www.jianshu.com/p/a90a3e617ba6 spr ...

  3. echart图表demo

    <!DOCTYPE html><html><head> <title>echarts</title></head><scr ...

  4. 解决UITextView滚动后无法显示完整内容

    滚动UITextView,偶尔内容只显示一半,现象如下

  5. 工具类BitMap 把网络URL图片转换成BitMap

    代码不复杂,直接把完整代码贴上. 这次是用到很旧的HttpURLConnection,那为什麽会用这个,因为我本来想转回okhttp的,可实在没时间转,项目就已经做下去了,结果转不回来. packag ...

  6. 0x06 - Nginx 负载均衡会话保持

    Nginx 负载均衡会话保持 背景 负载均衡时,如果APP需要保持特定状态的时候,就要保证同一用户的 session 会被分配到同一台服务器上. 实现方案 使用cookie 将用户的 session ...

  7. mysql模糊匹配like及批量替换replace

    1.mysql 模糊匹配 like 与 not like 用法 : SELECT * FROM `user` where `nickname` LIKE '%测试%' SELECT * FROM `u ...

  8. 动态添加checkbox

    <!--动态添加 checkbox--> <script type="text/javascript"> var data = new Array(); & ...

  9. GIL锁和进程/线程池

    GIL锁 1.GIL锁 全局解释器锁,就是一个把互斥锁,将并发变成串行,同一时刻只能有一个线程使用共享资源,牺牲效率,保证数据安全,也让程序员避免自己一个个加锁,减轻开发负担 带来的问题 感觉单核处理 ...

  10. 标题艺术与技术的完美结合,LG画廊OLED电视正式发布!

      由LG电子举办的"旷世巨作---面向未来的电视"主题沙龙于3月10号在王府井亚洲首家数字化奥迪展厅拉开帷幕.此次活动宣布了LG画廊OLED电视在国内市场上市.而我有幸参加了此次 ...