博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]formValidator的一些验证实例
阅读量:5260 次
发布时间:2019-06-14

本文共 3793 字,大约阅读时间需要 12 分钟。

原文

http://www.cnblogs.com/talk/archive/2012/01/29/2330887.html

$(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);
    }
});

转载于:https://www.cnblogs.com/huojing/articles/3897420.html

你可能感兴趣的文章
linux sed命令
查看>>
html标签的嵌套规则
查看>>
[Source] Machine Learning Gathering/Surveys
查看>>
HTML <select> 标签
查看>>
类加载机制
查看>>
tju 1782. The jackpot
查看>>
湖南多校对抗赛(2015.03.28) H SG Value
查看>>
hdu1255扫描线计算覆盖两次面积
查看>>
hdu1565 用搜索代替枚举找可能状态或者轮廓线解(较优),参考poj2411
查看>>
bzoj3224 splay板子
查看>>
程序存储问题
查看>>
Mac版OBS设置详解
查看>>
优雅地书写回调——Promise
查看>>
android主流开源库
查看>>
AX 2009 Grid控件下多选行
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
Ubuntu下面安装eclipse for c++
查看>>