首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

RegEx在jQuery Validator addMethod中

在jQuery Validator的addMethod中,RegEx表示正则表达式。正则表达式是一种用于匹配和处理字符串的强大工具。在jQuery Validator中,正则表达式可以用于自定义验证规则,以满足特定的验证需求。

在jQuery Validator的addMethod方法中,可以使用正则表达式来定义一个自定义验证规则。例如,以下代码定义了一个名为“myCustomRule”的自定义验证规则,该规则使用正则表达式来验证输入的电子邮件地址是否符合特定的格式:

代码语言:javascript
复制
$.validator.addMethod("myCustomRule", function(value, element) {
  return this.optional(element) || /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(value);
}, "Please enter a valid email address.");

在这个例子中,正则表达式/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/用于验证电子邮件地址是否符合常见的格式。

总之,在jQuery Validator的addMethod中,RegEx是一个非常有用的工具,可以用于自定义验证规则,以满足特定的验证需求。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • jquery_validation插件辅助资料

    jQuery Validate 客户端验证插件辅助资料 【】jQuery Validate网址 http://jqueryvalidation.org/ 【】jQuery validation引入 先引入jQuery,再引入jquery validation插件,以及支持I18N的插件文件 <script type="text/javascript" src="../jqueryvalidate/jquery.validate.min.js"></script> <script type="text/javascript" src="../jqueryvalidate/localization/messages_zh.min.js"></script> 【】jQuery validation插件的使用 1.定义表单 可以使用HTML form表单元素,也可以使用Struts2表单元素 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <s:form id="departmentaddform" action="../department/add.action" method="post"> <s:textfield id="departmentcode" name="dv.code" label="部门编码"></s:textfield> <s:textfield id="departmentname" name="dv.name" label="部门名称"></s:textfield> <s:submit value="提交"></s:submit> </s:form> 2.JavaScript引入jQuery validation  //部门表单验证  $("form#departmentaddform").validate({ rules: {        "dv.code": {            required: true,            rangelength: [5,10]        },        "dv.name":{ required: true        } }  }); 【】jQuery validation常用的验证规则 默认校验规则   (1)required:true               必输字段   (2)remote:"check.php"          使用ajax方法调用check.php验证输入值   (3)email:true                  必须输入正确格式的电子邮件   (4)url:true                    必须输入正确格式的网址   (5)date:true                   必须输入正确格式的日期   (6)dateISO:true                必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22 只验证格式,不验证有效性   (7)number:true                 必须输入合法的数字(负数,小数)   (8)digits:true                 必须输入整数   (9)creditcard:                 必须输入合法的信用卡号   (10)equalTo:"#field"           输入值必须和#field相同   (11)accept:                    输入拥有合法后缀名的字符串(上传文件的后缀)   (12)maxlength:5                输入长度最多是5的字符串(汉字算一个字符)   (13)minlength:10               输入长度最小是10的字符串(汉字算一个字符)   (14)rangelength:[5,10]         输入长度必须介于 5 和 10 之间的字符串")(汉字算一个字符)   (15)range:[5,10]               输入值必须介于 5 和 10 之间   (16)max:5                      输入值不能大于5   (17)min:10                     输入值不能小于10  【】验证规则的使用

    02

    为什么自定义函数效验器和页面加载成功事件不能放在一起

    !DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="../js/jquery-1.11.0.min.js" ></script> <script type="text/javascript" src="../js/jquery.validate.js" ></script> <script type="text/javascript" src="../js/messages_zh.js" ></script> <script> // 页面加载成功之后锁定,要加载的页面对象 $(function(){ // 锁定要效验的表单对象,调用validate方法 $("#formId").validate({ rules:{ card:{ required:true, cardLength:true } }, //提示信息 messages:{ card:{ cardLength:"请输入16位到18位的数字" } } }); }); //自定义函数效验器和页面加载成功事件不能放在一起,因为页面加载成功事件也是一个函数,两个函数不能相互嵌套 $.validator.addMethod("cardLength",function(val,ele,par){ if(par) { if(val.length == 16 || val.length == 18) { return true; } return false; }else { return true; } },"输入不合法"); </script> </head> <body> <form id="formId" action=""> 必填:<input type="text" name="username" />
    必填数字: <input type="text" name="password" />
    必填重复: <input type="text" name="repassword" />
    最小值: <input type="text" name="zuixiaozhi" />
    区间: <input type="text" name="shuzhiqujian" />
    身份证长度:<input type="text" name="card" />
    <input type="submit" value="提交" /> </form> </body> </html>

    06

    Install Jumpserver43

    Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/heatmap.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/heatmap.src.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/no-data-to-display.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/no-data-to-display.src.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/solid-gauge.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/modules/solid-gauge.src.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/dark-blue.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/dark-green.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/dark-unica.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/gray.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/grid-light.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/grid.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/sand-signika.js' Copying '/opt/jumpserver/apps/static/js/plugins/highcharts/themes/skies.js' Copying '/opt/jumpserver/apps/static/js/plugins/iCheck/icheck.min.js' Copying '/opt/jumpserver/apps/static/js/plugins/jstree/jstree.min.js' Copying '/opt/jumpserver/apps/static/js/plugins/layer/layer.js' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/layer.css' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/default/icon-ext.png' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/default/icon.png' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/default/loading-0.gif' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/default/loading-1.gif' Copying '/opt/jumpserver/apps/static/js/plugins/layer/skin/default/loading-2.gif' Copying '/opt/jumpserver/apps/static/js/plugins/magnific/jquery.magnific-popup.min.js' Copying '/opt/jumpserver/apps/static/js/plugins/metisMenu/jquery.metisMenu.js' Copying '/opt/jumpserver/apps/static/js/plugins/pace/pac

    01
    领券