首页 > 代码库 > js正则表达匹配&简单验证

js正则表达匹配&简单验证

技术分享
var common = {
    isNotNull:function(object){
        if(object == null || object == ‘‘ || object == undefined){
            return false;
        }
        return true;
    },
    MBToGB:function(mb){
        return Math.round(((mb / (1024))*100)/100);
    },
    myRound:function(number){
        var result = Math.round(number*10)/10;      
        if(result >= 0.5){
            result = Math.round(result);
        } 
        return result;
    },
    trimInputById:function(id){
        var input = $("#"+id);
        var result = $.trim(input.val());
        input.val(result);
        return result;
    },
    trimValueByObject:function(obj){
        var value =http://www.mamicode.com/ $.trim(obj.val());
        obj.val(value);
        return obj;
    },
    innerLabel:function(id,msg){
        var label = $("#msg"+id).html();
        if(label != null){
            common.innerLabelModify(id,msg);
        }
        $("#"+id).before(‘<label id="msg‘+id+‘" style="line-height:22px;height:22px;position:absolute;padding-left:8px;color:#bdbdbd;display:none;cursor:text;">‘+msg+‘</label>‘);
        if($("#"+id).val() == ""){
            $("#msg"+id).css("display","inline");
        }
        $("#"+id).focus(function(){
            $("#msg"+id).css("display","none");
        });
        $("#"+id).blur(function(){
            if($("#"+id).val() == ""){
                $("#msg"+id).css("display","inline");
            }
        });
        $("#msg"+id).click(function(){
            $("#msg"+id).css("display","none");
            $("#"+id).focus();
        });
    },
    innerLabelModify:function(id,msg){
        var label = $("#msg"+id);
        if(msg != undefined){
            label.html(msg);
        }
    },
    getVal:function(value,msg){
        if(value =http://www.mamicode.com/= undefined || value == null || value =http://www.mamicode.com/=""){
            if(msg != undefined){
                return msg;
            }
            return "&nbsp;";
        }
        return value;
    },
    trimSearchVal:function(formId){
        var vals = $("#"+formId+" input").each(function(){
            var val = $(this).val();
            $(this).val($.trim(val));
        });
    },
    /**
     *  
     * type:xml/str
     * xmlData: 对应type,xml传url地址,str穿str格式数据
     * treeId:要加入的div id 
     * enableCheckBoxes:1/0 是否有 多选框
     * enableThreeStateCheckboxes:1/0 是否允许三种状态的复选框(全选、不选、部分选中) 
     * handler:树的点击事件
     * 
     * */
    dhtmlxTree:function(type,xmlData,treeId,enableCheckBoxes,enableThreeStateCheckboxes,handler){
        $("#"+treeId).empty();
        var tree;
        tree = new dhtmlXTreeObject(treeId, "100%", "100%", 0);
        tree.setSkin(‘dhx_skyblue‘);
        tree.setImagePath(ctx+"/common/dhtmlxTree/common/images/");
        tree.enableCheckBoxes(enableCheckBoxes); 
        tree.enableThreeStateCheckboxes(enableThreeStateCheckboxes);
        tree.setOnCheckHandler(handler);
        if(type=="xml"){
            tree.loadXML(xmlData); 
        }else{
            tree.loadXMLString(xmlData);
        }
        return tree;
    },

    /**
     *  
     * treeid:ul标签Id
     * data: json 数据
     * onclick:点击事件( 可选 )
     * enable:true/false ( 默认true )
     * setting:自定义  ( 可选 )
     * api地址:http://www.baby666.cn/v3/api.php
     * */
    zTree:function( treeid  , data, onclick , enable ,  setting,fontCss){
        
        if( !common.isNotNull(setting) ){
            
          setting = {
                  callback: {
                      onClick: common.isNotNull(onclick)?onclick:null
                  },
                check: {
                    
                    enable:enable==undefined?true:enable
                            
                },
                data: {
                    
                    simpleData: {
                        
                        enable: true
                        
                    }
                },
                view: {
                    fontCss:  common.isNotNull(fontCss)?fontCss:getFont,
                    nameIsHTML: true
                }
            };
        }
        var tree = $.fn.zTree.init( $("#"+treeid) , setting , eval("["+data +"]") );
        
        return tree ;
    }
}
一般简单验证
技术分享
var validate = {
    //基础方法
    isTrue:function(object,reg){
        return reg.test(object.val());
    },
    //名称
    isName:function(name){
        var reg = /^[a-zA-Z0-9\u4e00-\u9fa5_]{1,32}$/;
        return this.isTrue(name,reg);
    },
    //验证长度
    isNameLength:function(name){
        var namel=getCharLength(name.val());
        if(0<namel>300){return false;
        }else{
            return true;
        }
    },
    //验证备注或描述长度
    isDescriptionLength:function(description){
        var namel=getCharLength(description.val());
        if(namel>63){return false;
        }else{
            return true;
        }
    },
    //用户名
    isUserName:function(userName){
        var reg =  /^[0-9a-zA-Z-_]{2,16}$/;
        return this.isTrue(userName,reg);
    },
    //管理员用户名
    isUserNameNew:function(userName){
        var reg = /^[a-zA-Z0-9][a-zA-Z0-9_@.-]{5,31}$/;
        return this.isTrue(userName,reg);
    },
    //密码
    isPassWord:function(passWord){
        var reg = /^[\w~!@#$%^&*]{6,16}$/;
        return this.isTrue(passWord,reg);
    },
    //邮箱
    isEmail:function(email){
        var reg = /^([a-zA-Z0-9_\.\-]{1,30})+\@(([a-zA-Z0-9\-]{1,10})+\.)+([a-zA-Z0-9]{2,4})+$/;
        return this.isTrue(email,reg);
    },
    //URL
    isURL:function(url){
        var reg = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
        return this.isTrue(url,reg);
    },
    //ip地址
    isIp:function(ip){
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/; 
        return this.isTrue(ip,reg);
    },
    
    //带端口的ip地址
    isPortIp:function(ip){
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\:([0-9]+)$/; 
        return this.isTrue(ip,reg);
    },
    
    //验证是否为字母
    isEn:function(en){
        var reg = /^[a-zA-Z]+$/;
        return this.isTrue(en,reg);
    },
    //验证是否为port
    isPort:function(en){
        var reg = /^[0-9]+$/;
        return this.isTrue(en,reg);
    },
    //验证是否为汉字
    isCn:function(cn){
        var reg = /^[\u4e00-\u9fa5]+$/;
        return this.isTrue(cn,reg);
    },
    //验证MSN
    isMSN:function(msn){
        var reg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
        return this.isTrue(msn,reg);
    },
    //验证QQ号码
    isQQ:function(qq){
        var reg = /^[1-9]\d{4,9}$/;
        return this.isTrue(qq,reg);
    },
    //验证手机号码
    isMobile:function(mobile){
        var reg = /^((\(\d{3}\))|(\d{3}\-))?1[3,4,5,6,8]\d{9}$/;
        return this.isTrue(mobile,reg);
    },
    //验证传真
    isFox:function(fox){
        var reg = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
        return this.isTrue(fox,reg);
    },
    //验证固定电话
    isTelephone:function(telephone){
        var reg = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
        return this.isTrue(telephone,reg);
    },
    //验证邮编
    isZipCode:function(zipCode){
        var reg = /^[0-9]{6}$/;
        return this.isTrue(zipCode,reg);
    },
    //验证身份证号码
    isIdCard:function(idCard){
        var reg = /(^([\d]{15}|[\d]{18}|[\d]{17}[xX]{1})$)/;
        return this.isTrue(idCard,reg);
    },//checkType=1,既需要验证空,也要验证格式,此时要传入EmptyTipsText参数
    //checkType=2,不需要验证空,但要验证格式
    //checkType、EmptyTipsText都忽略则只使用正则去验证
    
    //验证名称:长度3-32位,以字母开头,支持字母、数字、中划线- 与下划线_ 组合
    isObjectName:function(nameCode){
        var reg = /^[a-zA-Z0-9\_-]{3,32}$/;;
        return this.isTrue(nameCode,reg);
    },
    
    isDateFormat:function(date){
        var reg =  /^[0-9]{4}\/[01]{0,1}[0-9]\/[0-3]{0,1}[0-9]{0,1}\s*((\u4E0A\u5348)|(\u4E0B\u5348))[0-2]{0,1}[0-9]{0,1}(:[0-6]{0,1}[0-9]){2}$/;
        return this.isTrue(date,reg);
    },
    
    isOnlyNum:function(date){
        var reg =  /^[0-9]+(.[0-9]{1,})?$/;
        return this.isTrue(date,reg);
    },
    
    isOnlyTwoNumAfterPoint:function(date){
        var reg =  /^[0-9]+(.[0-9]{1,2})?$/;
        return this.isTrue(date,reg);
    },
    
    checkByReg:function(jqObj,tipsText,reg,checkType,EmptyTipsText){
        if(checkType==1&&!common.isNotNull(jqObj,EmptyTipsText)){
            return false;
        }
        if(checkType==2&&!common.isNotNull(jqObj)){
            return true;
        }
        if(!reg.test(jqObj.val())){
            jqObj.after("<span name=‘sp_error_tips‘ style=‘color:red;‘><br/>"+tipsText+"</span>");
            return false;
        }
        return true;
    }
}
正则匹配

 

js正则表达匹配&简单验证