1. 程式人生 > >js 參數校驗器

js 參數校驗器

福建 整數 pan 移動端 環境 web value sem fin

//校驗器
var validate = {
    //校驗當前運行環境是否是手機端
    isWap:function(){
        var sUserAgent= navigator.userAgent.toLowerCase(); 
         var bIsIpad= sUserAgent.match(/ipad/i) == "ipad"; 
         var bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os"; 
         var bIsMidp= sUserAgent.match(/midp/i) == "midp"; 
         
var bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc= sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid= sUserAgent.match(/android/i) == "android"; var bIsCE= sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { return true;//是手機端 } else { return false;//不是手機端 } }, //身份證校驗 identityCodeValid:function(code){ var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"內蒙古",21:"遼寧",22:"吉林",23:"黑龍江 ",31:"上海",32:"江蘇",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山東",41:"河南",42:"湖北 ",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",51:"四川",52:"貴州",53:"雲南",54:"西藏 ",61:"陜西",62:"甘肅",63:"青海",64:"寧夏",65:"新疆",71:"臺灣",81:"香港",82:"澳門",91:"國外 "};
var pass= true; if (!code || !/^[1-9]\d{5}((1[89]|20)\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dx]$/i.test(code)) { console.log("身份證號格式錯誤"); pass = false; }else if(!city[code.substr(0,2)]){ console.log("地址編碼錯誤"); pass = false; }else{ //18位身份證需要驗證最後一位校驗位 if(code.length == 18){ code = code.split(‘‘); //∑(ai×Wi)(mod 11) //加權因子 var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]; //校驗位 var parity = [ 1, 0, ‘X‘, 9, 8, 7, 6, 5, 4, 3, 2 ]; var sum = 0; var ai = 0; var wi = 0; for (var i = 0; i < 17; i++) { ai = code[i]; wi = factor[i]; sum += ai * wi; } var last = parity[sum % 11]; if(parity[sum % 11] != code[17].toUpperCase()){ console.log("校驗位錯誤"); pass =false; } } } return pass;//true 表示校驗通過;false 表示校驗失敗 }, //檢查生日是否正確,如果正確則返回正確的生日 checkBirthday : function(card) { var len = card.length; //身份證15位時,次序為省(3位)市(3位)年(2位)月(2位)日(2位)校驗位(3位),皆為數字 if(len == ‘15‘) { var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; var arr_data = card.match(re_fifteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date(‘19‘+year+‘-‘+month+‘-‘+day); return verifyBirthday(‘19‘+year+"-"+month+"-"+day); } //身份證18位時,次序為省(3位)市(3位)年(4位)月(2位)日(2位)校驗位(4位),校驗位末尾可能為X if(len == ‘18‘) { var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; var arr_data = card.match(re_eighteen); var year = arr_data[2]; var month = arr_data[3]; var day = arr_data[4]; var birthday = new Date(year+‘-‘+month+‘-‘+day); return (year+"-"+month+"-"+day); } return false; },//校驗姓名 isName:function(value){ // if(value.match(/^([\u4e00-\u9fa5]|[0-9a-zA-Z]|[.])+$/)){ //if(value.match(/^([\u4e00-\u9fa5]{2,10})+$/)){ return true; //} //else {return false; } },//校驗英文 isEnName:function(value){ if(value.match(/^([0-9a-zA-Z]|[.]){1,30}$/)){ return true; }else{ return false; } },//校驗數字 isNum:function(s){ var patrn=/^[0-9]{1,20}$/; if (!patrn.exec(s)) return false return true ; },//校驗護照 isPassport:function(value){ // /(P\d{7})|(G\d{8})/ /*if(value.match(/^([0-9a-zA-Z]{8}|[0-9a-zA-Z]{9})$/)){*/ return true; /* }else{ return false; }*/ },//校驗地址 isAddress:function(value){ if(value.match(/^([0-9a-zA-Z]|[\u4e00-\u9fa5]|[-.\(\)]){2,100}$/)){ return true; }else{ return false; } },//校驗手機號 isMobile:function(value){ if(value == ‘‘){ return false; }else if(value.trim().match(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/)){ return true; }else{ return false; } }, //校驗郵箱 isEmail:function(email){ if(email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){ return true; }else{ return false; } },//檢查數組元素是否有重復,true表示重復;false表示不重復 validateRep:function(arr){ return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+arr.join("\x0f\x0f") +"\x0f"); },isImage:function(imgPath){ imgPath="abc123"+imgPath.substring(imgPath.lastIndexOf("."),imgPath.length); return validate.isNotNull(imgPath)&&/^.*[^a][^b][^c]\.(?:png|jpg|bmp|gif|jpeg)$/.test(imgPath.toLocaleLowerCase()); }, isMoney:function(money){ var reg= /^-?\d+\.?\d{0,2}$/; return reg.test(money); },isUndefined:function(obj){ return "undefined" == typeof obj||obj==undefined; },isNotNull:function(value){ return value!=null&&$.trim(value)!=""; },isNull:function(value){ return !validate.isNotNull(value); },isURL:function (str){ return!!str.trim().match(/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g); },isInt:function(num){ num=$.trim(num); if(num=="-"){ return true; } var reg=/^-?[0-9]\d*$/; return reg.test(num); } }

//判定是否是移動端
function isMobile() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid
            || bIsCE || bIsWM) {
        return true;
    } else {
        return false;
    }
}
//判定是否是微信瀏覽器
function isWeixin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == ‘micromessenger‘)
        return true;
    else
        return false;
}
/**
 * cookie 操作
 */
var _cookie = {
        //獲取cookie生命周期,i=1表示1天;
        getExpTime:function(i){
            var exp  = new Date(); 
            return new Date().getTime() + i*24*60*60*1000;
        },
        //設值
        set:function(name,value,expTime){
            var exp  = new Date();
            exp.setTime(expTime);
            document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
        },
        //獲取值
        get:function(name){
            var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
            if(arr != null) return unescape(arr[2]); return null;
        },
        //刪除指定值
        del:function(name){
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval=_cookie.get(name);
            if(cval!=null) {document.cookie= name + "="+cval+";expires="+exp.toGMTString()};
        }
}
var bindUtil={
        bindOnlyInputInt:function (obj,funObj){//只能輸入整數
            var tempMoneyValue=validate.isInt($(obj).val())?$(obj).val():"";
            $(obj).bind("input propertychange",function() {
                if(!validate.isInt(this.value)){
                    if(!validate.isNotNull(this.value)&&this.value.length==0){
                        tempMoneyValue="";
                    }else{
                        this.value=tempMoneyValue;
                    }
                }else{
                    tempMoneyValue=$.trim(this.value);
                    if(this.value.length!=tempMoneyValue.length){
                        this.value=tempMoneyValue;
                    }
                }
                var thisObj=this;
                if(!validate.isUndefined(funObj)){
                    funObj(thisObj);
                }
            });        
        },
        bindOnlyInputMoney: function (inputObj,funObj){//只能輸入金額
            var tempMoneyValue=validate.isMoney($(inputObj).val())?$(inputObj).val():"";;
            $(inputObj).bind("input propertychange",function() {
                if(!validate.isMoney(this.value)){
                    if(!validate.isNotNull(this.value)&&this.value.length==0){
                        tempMoneyValue=this.value;
                    }else{
                        this.value=tempMoneyValue;
                    }
                }else{
                    tempMoneyValue=$.trim(this.value);
                    if(tempMoneyValue.length!=this.value.length){
                        this.value=tempMoneyValue;
                    }
                }
                var thisObj=this;
                if(!validate.isUndefined(funObj)){
                    funObj(thisObj);
                }
            });
        }
}

js 參數校驗器