formValidator的一些验证实例

$(function () {
    try {
        $.formValidator.initConfig({
            formid: "formTable",
            errorfocus: false,
            submitonce: true,
            tipstyle: "both",
            onerror: function () { // 验证不通过时的回调函数
                alert("红色提示处输入非法,请根据提示修改!");
            }
        });
        //验证字符串(必填)
        $("#name").formValidator({ // 验证:模块名称
            onshow: "(必填)",
            onfocus: "(必填)不超过50个字符",
            oncorrect: "(正确)"
        }).inputValidator({
            min: 1,
            max: 50,
            onerrormin: "(错误)不能为空",
            onerrormax: "(错误)不超过50个字符,汉字算两个字符"
        });

        //验证字符串(选填)
        $("#name").formValidator({ // 验证:模块名称
            onshow: "(选填)",
            onfocus: "(选填)不超过50个字符",
            oncorrect: "(正确)",
            empty: true
        }).inputValidator({
            min: 1,
            max: 50,
            onerrormin: "(错误)不能为空",
            onerrormax: "(错误)不超过50个字符,汉字算两个字符"
        });

        //验证时间
        $("#addDate").formValidator({ // 验证:发送时间
            onshow: "(必填)",
            onfocus: "(必填)请选择操作时间",
            oncorrect: "(正确)"
        }).functionValidator({
            fun: function (val, elem) {
                if (!/^\d{4}-\d{2}-\d{2}[ ]\d{2}:\d{2}$/.test(val)) {
                    return "(错误)请选择操作时间";
                }
                return true;
            }
        });

        //ajax验证
        $("#account").formValidator({ // 验证:模块名称
            onshow: "(必填)",
            onfocus: "(必填)不超过50个字符",
            oncorrect: "(正确)"
        }).inputValidator({
            min: 1,
            max: 50,
            onerrormin: "(错误)不能为空",
            onerrormax: "(错误)不超过50个字符,汉字算两个字符"
        }).ajaxValidator({
            type: "post",
            url: "EnterpriseManage!ajaxValidatorUserAccount.action",
            success: function (data) {
                if (data == "0") {
                    return true;
                } else if (data == "1") {
                    return false;
                }
            },
            onerror: "该账号已被占用,请更换!"
        });

        //密码及重复密码验证
        $("#password").formValidator({ // 验证:模块名称
            onshow: "(必填)",
            onfocus: "(必填)不超过11个字符",
            oncorrect: "(正确)"
        }).inputValidator({
            min: 1,
            max: 50,
            onerrormin: "(错误)不能为空",
            onerrormax: "(错误)不超过11个字符,汉字算两个字符"
        });

        $("#passwordRepeat").formValidator({
            onshow: "(必填)",
            onfocus: "(必填)2次密码必须一致",
            oncorrect: "(正确)"
        }).compareValidator({
            desid: "password",
            operateor: "=",
            onerror: "(错误)2次密码不一致,请确认"
        });

        //图片格式验证
        $("#tcCodeLogo").formValidator({
            onshow: "(选填)",
            onfocus: "(选填)请上传图片文件",
            oncorrect: "(正确)",
            empty: true
        }).regexValidator({
            regexp: regexEnum.picture,
            onerror: "只能上传图片文件"
        });

        //数值验证
        $("#nameNum").formValidator({ // 验证:模块名称
            onshow: "(必填)",
            onfocus: "(必填)值1到50",
            oncorrect: "(正确)"
        }).inputValidator({
            min: 1,
            max: 50,
            type: "value",
            onerrormin: "(错误)不能为空",
            onerrormax: "(错误)值1到50"
        });
        //电话验证
        $("#linkPhone").formValidator({
            onshow: "(选填)",
            onfocus: "(选填)",
            oncorrect: "(正确)",
            empty: true
        }).regexValidator({
            regexp: "^(\\d{3,4}-?\\d{7,8}|(13|15|18)\\d{9})$",
            onerror: "(错误)电话号码格式不正确,请检查"
        });

        //EMail验证
        $("#linkEmail").formValidator({
            onshow: "(选填)",
            onfocus: "(选填)请选择正确EMail格式",
            oncorrect: "(正确)",
            empty: true
        }).regexValidator({
            regexp: regexEnum.email,
            onerror: "(错误)Email格式不正确,请检查"
        });

        //select验证
        $("#testSelect").formValidator({
            onshow: "(必填)",
            onfocus: "(必填)请选择选项",
            oncorrect: "(正确)"
        }).inputValidator({
            min: 0,  //开始索引
            onerror: "你是不是忘记选择学历了!"
        });

        //隐藏时,默认验证通过
        $("#smsProductName").formValidator({ // 验证
            onshow: "(必填)",
            onfocus: "(必填)不超过50个字符,汉字算两个字符",
            oncorrect: "(正确)"
        }).functionValidator({
            fun: function (val, elem) {
                if ($("#smsProductName").is(":hidden")) {
                    return true;
                }
                if (!/^\S{1,50}$/.test(val)) {
                    return "(错误)不超过50个字符,汉字算两个字符";
                }
                return true;
            }
        });
        //多选选择框的验证方式 略有点复杂了
        $(":checkbox[name='productType']").formValidator({
            onshow: "(至少选择一个)",
            onfocus: "(至少选择一个)",
            oncorrect: "(正确)"
        }).functionValidator({
            fun: function (val, elem) {
                var objs = $(":checkbox[name='productType']");
                for (var i = 0; i < objs.length; i++) {
                    if ($(objs[i]).attr("checked") == true) {
                        $('#productTypeTip').removeClass();
                        $('#productTypeTip').addClass("onSuccess");
                        $('#productTypeTip').html();
                        $('#productTypeTip').html("<nobr>正确</nobr>");
                        return true;
                    }
                }
                $('#productTypeTip').removeClass();
                $('#productTypeTip').addClass("onError");
                $('#productTypeTip').html();
                $('#productTypeTip').html("<nobr>(至少选择一项)</nobr>");
                return false;
            }
        });
    } catch (e) {
        alert(e);
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值