zoukankan      html  css  js  c++  java
  • jquery-validae

    $(document).ready(function() {
    jQuery.validator.addMethod("realName", function(value, element) {
    var tel = /^([u4e00-u9fa5]+|([a-zA-Z]+s?)+)$/;
    return this.optional(element) || (tel.test(value));
    }, "请正确填写您的姓名");
    jQuery.validator.addMethod("realWechatNum", function(value, element) {
    var tel = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])d{8}$/;
    var rex = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/;
    return this.optional(element) || (rex.test(value)) || (tel.test(value));
    }, "微信号只能含有数字、字母、下划线、和减号,且长度在6-20位,开头为字母/手机号为11位数字");
    jQuery.validator.addMethod("integer", function(value, element) {
    var tel = /^[1-9]*[1-9][0-9]*$/;
    return this.optional(element) || (tel.test(value));
    }, "请输入正确的员工编号");
    $("#inputForm").validate({
    onfocusout: false,
    rules: {
    "staff.staffName": {realName: true},
    "staff.staffWorkNo": {remote: "${ctx}/sys/user/checkStaffWorkNo?staffId=${sysUser.staffId}"},
    "staff.staffNo": {integer:true,maxlength:8},
    wechatNum: {
    realWechatNum:true,
    remote: {
    type: "post",
    url: "${ctx}/sys/user/checkWechatNum?userId=${sysUser.userId}",
    data: {
    wechatNum: function(){return $("#wechatNum").val();}
    }
    }
    }
    },
    messages: {
    "staff.staffWorkNo": {remote:"员工档案编码重复"},
    wechatNum: {
    realWechatNum: "微信号只能含有数字、字母、下划线、和减号,且长度在6-20位,开头为字母/手机号为11位数字",
    remote: "微信号/手机号已存在"
    },
    "staff.staffNo": {
    integer:"编号为非0开头正整数数字格式并且最多8位",
    maxlength:"编号为非0开头正整数数字格式并且最多8位",
    },
    },
    submitHandler: function(form){
    $('#myModal').modal({backdrop: 'static', keyboard: false});
    loading('正在提交,请稍等...');
    form.submit();
    },
    errorContainer: "#messageBox",
    errorPlacement: function(error, element) {
    $("#messageBox").text("输入有误,请先更正。");
    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
    error.appendTo(element.parent().parent());
    } else {
    error.insertAfter(element);
    }
    }
    });
    });

    其中jQuery.validate的optional(element),用于表单控件的值不为空时才触发验证。
    当element为空时this.optional(element)=true,用于在该控件为非必填项目时可以通过验证,及条件可以不填但是不能填错格式。

    如果值为空时也要触发验证,移除optional(element)。

  • 相关阅读:
    UVALIVE 4819 最大流
    Directx 3D编程实例:随机绘制的立体图案旋转
    PHP漏洞全解(四)-xss跨站脚本攻击
    PHP漏洞全解(三)-客户端脚本植入
    PHP漏洞全解(二)-命令注入攻击
    PHP漏洞全解(一)-PHP网站的安全性问题
    BT5下安装Metasploit4.5方法
    Ubuntu使用apt-get安装本地deb包
    Linux按照时间查找文件
    Linux系统备份与还原
  • 原文地址:https://www.cnblogs.com/h-wei/p/10149790.html
Copyright © 2011-2022 走看看