首页 > 代码库 > js 参数校验器
js 参数校验器
//校验器 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 =http://www.mamicode.com/ 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 =http://www.mamicode.com/ 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 =http://www.mamicode.com/= ‘‘){ 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=http://www.mamicode.com/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=http://www.mamicode.com/tempMoneyValue; } }else{ tempMoneyValue=$.trim(this.value); if(this.value.length!=tempMoneyValue.length){ this.value=http://www.mamicode.com/tempMoneyValue; } } var thisObj=this; if(!validate.isUndefined(funObj)){ funObj(thisObj); } }); }, bindOnlyInputMoney: function (inputObj,funObj){//只能输入金额 var tempMoneyValue=http://www.mamicode.com/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=http://www.mamicode.com/tempMoneyValue; } }else{ tempMoneyValue=$.trim(this.value); if(tempMoneyValue.length!=this.value.length){ this.value=http://www.mamicode.com/tempMoneyValue; } } var thisObj=this; if(!validate.isUndefined(funObj)){ funObj(thisObj); } }); } }
js 参数校验器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。