验证插件

/**
 * Created by Administrator on 2015/8/27.
 */
$(function(){
    //验证插件
    var ValidBidForm = {
        formid:"form_id",
        eleresult:"span.sp-result:first",
        isAccess:false,     //是否正在访问
        errArray:[],        //错误数组
        iptList:{    //无slt_开头均为文本框 id  , 含有slt_开头为select下拉框 的class
            bidtitle:{
                regex:regexCollection.notempty,
                onFocus:"请输入招标标题",
                onKeyup:"招标标题不能为空"
            },
            bidDeadline1:{
                regex:regexCollection.mDate,
                onFocus:"请选择投标截止时间",
                onKeyup:"投标截止时间不能为空"
            },
            bidResultDate1:{
                regex:regexCollection.mDate,
                onFocus:"请选择公布结果时间",
                onKeyup:"公布结果时间不能为空"
            },
            slt_contactperson:{
                regex:regexCollection.notempty,
                onChange:"联系人必选"
            }
        },
        userDefineValid:function(){//用户自定义验证方法
            var _this = this;
            //商品名称
            $(".tb-bidproinfo tbody").delegate(".prodName","focusin",function(){
                _this.getfocus($(this),"请输入商品名称");
            });
            $(".tb-bidproinfo tbody").delegate(".prodName","blur",function(){
                $(this).trigger("keyup");
            });
            $(".tb-bidproinfo tbody").delegate(".prodName","keyup",function(){
                if(!$.trim($(this).val()).match(regexCollection.notempty)){
                    ValidBidForm.whenError($(this),"商品名称不能为空");
                    ValidBidForm.errArray.push($(this));
                }else{
                    ValidBidForm.whenCorrect($(this));
                }
            });
            //所属行业
            $(".tb-bidproinfo tbody").delegate(".regionclass .catgyId","blur",function(){
                if(!$.trim($(this).val()).match(regexCollection.notempty) || $(this).parent().find("label").html()==""){
                    ValidBidForm.whenError($(this).parent(".regionclass"),"所属行业必选");
                    ValidBidForm.errArray.push($(this));
                }else{
                    ValidBidForm.whenCorrect($(this).parent(".regionclass"));
                }
            });
            //数量
            $(".tb-bidproinfo tbody").delegate(".prodQuantity","focusin",function(){
                _this.getfocus($(this),"请输入数量");
            });
            $(".tb-bidproinfo tbody").delegate(".prodQuantity","blur",function(){
                $(this).trigger("keyup");
            });
            $(".tb-bidproinfo tbody").delegate(".prodQuantity","keyup",function(){
                if(!$.trim($(this).val()).match(regexCollection.mNum)){
                    ValidBidForm.whenError($(this),"只能为非负整数");
                    ValidBidForm.errArray.push($(this));
                }else{
                    ValidBidForm.whenCorrect($(this));
                }
            });
            //收货地
            $(".tb-list-box tbody .recivaddress .deliverAddrId").bind("blur",function(){
                if(!$.trim($(this).val()).match(regexCollection.notempty) || $(this).parent().find("label").html()==""){
                    ValidBidForm.whenError($(this).parent(".recivaddress"),"收货地必选");
                    ValidBidForm.errArray.push($(this));
                }else{
                    ValidBidForm.whenCorrect($(this).parent(".recivaddress"));
                }
            });

            //电话
            $(".tb-list-box tbody .ipttel").bind("focusin",function(){
                _this.getfocus($(this),"请输入加区号的固定电话");
            });
            $(".tb-list-box tbody .ipttel").bind("blur",function(){
                $(this).trigger("keyup");
            });
            $(".tb-list-box tbody .ipttel").bind("keyup",function(){
                var $mobile = $(".tb-list-box tbody .iptphone");
                if(!$.trim($(this).val()).match(regexCollection.tel) && $.trim($(this).val())!=""){
                    ValidBidForm.whenError($(this),"固定电话格式错误");
                    ValidBidForm.errArray.push($(this));
                }else{
                    if($.trim($(this).val())=="" && $.trim($($mobile).val()) == ""){
                        ValidBidForm.whenError($(this),"");
                        ValidBidForm.whenError($($mobile),"固定电话和手机必须选填一个");
                        ValidBidForm.errArray.push($(this));
                    }else if(!$.trim($($mobile).val()).match(regexCollection.mobile) && $.trim($($mobile).val())!=""){
                    ValidBidForm.whenError($($mobile),"手机号格式错误");
                        ValidBidForm.errArray.push($($mobile));
                    }else{
                        ValidBidForm.whenCorrect($(this));
                        ValidBidForm.whenCorrect($($mobile));
                    }
                }
            });
            //手机
            $(".tb-list-box tbody .iptphone").bind("focusin",function(){
                _this.getfocus($(this),"请输入手机号");
            });
            $(".tb-list-box tbody .iptphone").bind("blur",function(){
                $(this).trigger("keyup");
            });
            $(".tb-list-box tbody .iptphone").bind("keyup",function(){
                var $tel = $(".tb-list-box tbody .ipttel");
                if(!$.trim($(this).val()).match(regexCollection.mobile) && $.trim($(this).val())!=""){
                    ValidBidForm.whenError($(this),"手机号格式错误");
                    ValidBidForm.errArray.push($(this));
                }else{
                    if($.trim($(this).val())=="" && $.trim($($tel).val()) == ""){
                        ValidBidForm.whenError($(this),"");
                        ValidBidForm.whenError($($tel),"固定电话和手机必须选填一个");
                        ValidBidForm.errArray.push($(this));
                    }else if(!$.trim($($tel).val()).match(regexCollection.tel) && $.trim($($tel).val())!=""){
                    ValidBidForm.whenError($($tel),"固定电话格式错误");
                        ValidBidForm.errArray.push($($tel));
                    }else{
                        ValidBidForm.whenCorrect($(this));
                        ValidBidForm.whenCorrect($($tel));
                    }
                }
            });
        },
        validFormData:function(){
            //验证数据
            for(var item in this.iptList){
            if(item.toString().indexOf("slt_")!=-1){
                this.sltHandler(item);
            }else{
                if(item != undefined) $("#"+item).trigger("keyup");
            }
                
            }
            
            //用户自定义验证
            $(".tb-bidproinfo tbody .prodName").trigger("keyup");   //商品名称
            $(".tb-bidproinfo tbody .regionclass .catgyId").trigger("blur");    //所属行业
            $(".tb-bidproinfo tbody .prodQuantity").trigger("keyup");   //数量
            $(".tb-list-box tbody .recivaddress .deliverAddrId").trigger("blur");   //收货地
            //$("#mySelectContactPerson").trigger("change");  //联系人
            $(".tb-list-box tbody .ipttel").trigger("keyup");   //固定电话
            $(".tb-list-box tbody .iptphone").trigger("keyup"); //手机


        },
        getfocus:function(obj,des){
            this.funBorderDefault(obj,des);
        },
        whenError:function(obj,des){
            this.funBorderError(obj,des);
        },
        whenCorrect:function(obj){
            this.funBorderCorrect(obj);
        },
        validSubmit:function(funback){    //表单提交
            var _this = this;
            if (_this.isAccess == false){
                _this.isAccess = true;
                try{
                    _this.errArray.length=0;
                    _this.validFormData();
                    if(_this.errArray.length>0){
                        _this.isAccess = false;
                        return false;
                    }
                    _this.isAccess = false;
                    if(funback) funback();
                    return true;
                }catch(e){
                    _this.isAccess = false;
                    return false;
                }
            }
            return false;
        },
        funFocusCorrectStyle:function(obj,hasBorder){
            (hasBorder==true) ? obj.css({ "background-color": "#fef3e6", "border": "1px solid #e4b884" }) : obj.css({ "background-color": "#fef3e6" });
        },
        funcFocusErrorStyle:function(obj, hasBorder) {
            (hasBorder == true) ? obj.css({ "background-color": "#f6dbe7", "border": "1px solid #f73d41" }) : obj.css({ "background-color": "#f6dbe7" });
        },
        funFocusDefaultStyle:function(obj, hasBorder) {
            (hasBorder == true) ? obj.css({ "background-color": "#ffffff", "border": "1px solid #d2d2d2" }) : obj.css({ "background-color": "#ffffff" });
        },
        funBorderError:function(obj,des) {
            this.funcFocusErrorStyle($(obj), true);
            $(obj).nextAll(this.eleresult).html(des).css({ "color":"#ff0000" });;
        },
        funBorderCorrect:function(obj) {
            this.funFocusCorrectStyle($(obj), true);
            $(obj).nextAll(this.eleresult).html("");
        },
        funBorderDefault:function(obj,des) {
            this.funFocusDefaultStyle($(obj), true);
            if(des!="")
                $(obj).nextAll(this.eleresult).html(des).css({ "color":"#999999" });
            else
                $(obj).nextAll(this.eleresult).html("");
        },
        initEvent:function(){//绑定事件
            for(var item in ValidBidForm.iptList){  //绑定静态输入框事件
                if(item != undefined){
                    if(item.indexOf("slt_")!=-1){
                        $("."+item).bind("change",{index: item},this.changeHandler);
                    }else{
                            $("#"+item).bind("focusin",{index: item},this.focusinfHandler);
                            $("#"+item).bind("blur",{index: item},this.blurHandler);
                            $("#"+item).bind("keyup",{index: item},this.keyupHandler);
                        }
                }
            }
            this.userDefineValid(); //用户自定义绑定事件
        },
        focusinfHandler:function(event){
            ValidBidForm.getfocus($("#"+event.data.index),ValidBidForm.iptList[event.data.index].onFocus);
        },
        blurHandler:function(event){
            $("#"+event.data.index).trigger("keyup");
        },
        keyupHandler:function(event){
            if(!$.trim($("#"+event.data.index).val()).match(ValidBidForm.iptList[event.data.index].regex)){
                ValidBidForm.whenError($("#"+event.data.index),ValidBidForm.iptList[event.data.index].onKeyup);
                ValidBidForm.errArray.push(event.data.index.toString());
            }else{
                ValidBidForm.whenCorrect("#"+event.data.index);
            }
        },
        changeHandler:function(event){
            if(!$.trim($("."+event.data.index).val()).match(ValidBidForm.iptList[event.data.index].regex)){
                ValidBidForm.whenError($("."+event.data.index),ValidBidForm.iptList[event.data.index].onChange);
                ValidBidForm.errArray.push(event.data.index.toString());
            }else{
                ValidBidForm.whenCorrect("."+event.data.index);
            }
        },
        sltHandler:function(_item){
            if(!$.trim($("."+_item).val()).match(ValidBidForm.iptList[_item].regex)){
                ValidBidForm.whenError($("."+_item),ValidBidForm.iptList[_item].onChange);
                ValidBidForm.errArray.push(_item.toString());
            }else{
                ValidBidForm.whenCorrect("."+_item);
            }
        }
    };
    //初始化绑定事件
    ValidBidForm.initEvent();
    //获得焦点
    $("#"+ValidBidForm.formid).submit(function(){
        if(ValidBidForm.validSubmit(function(){
                //回调函数
                //alert("提交成功");
            })){
            return true;
        }
        else{
            return false;
        }
    });
});

posted @ 2015-10-08 15:56  周佳洪  阅读(222)  评论(0编辑  收藏  举报