标签:
列出一些常用的格式验证的js方法,以备查询。
// 检查是否为数字 function checkIsInteger(str){ if (str == ""){ return false; } if (str.search(/^[0-9]+$/) < 0){ return false; } else{ return true; } } //检查是否整数 function CheckNum(str){ if(!str || isNaN(str)){ return false; } str=(str * 1).toString(); if(str.indexOf(".") == -1){ return false; } return true; } // 检查是否为有效的密码,密码只允许由ascii组成,此函数只在修改或注册密码时使用 function checkValidPasswd(str){ var reg = /^[x00-x7f]+$/; if (! reg.test(str)){ return false; } if (str.length < 6 || str.length > 16){ return false; } return true; } // 检查是否为中文 function isChn(str){ var reg = /^[u4E00-u9FA5]+$/; if(!reg.test(str)){ return false; } return true; } // 检查是否为有效的真实姓名,只能含有中文或大写的英文字母 function isValidTrueName(strName){ var str = Trim(strName); //判断是否为全英文大写或全中文,可以包含空格 var reg = /^[A-Z u4E00-u9FA5]+$/; if(reg.test(str)){ return false; } return true; } //检查邮箱 function isEmail(str){ var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/; return reg.test(str); } //检查电话号码 function isTel(str){ var reg=/^([0-9]|[\-])+$/g ; if(str.length<7 || str.length>18){ return false; } else{ return reg.exec(str); } } //检查年龄 function isAge(str){ var mydate=new Date; var now=mydate.getFullYear(); if (str < now-60 || str > now-18){ return false; } return true; } //是否为合法的日期格式:YYYY-MM-DD function isDateFormat(txt){ if(txt==null || txt == ""){return false;} else{ var regex = /[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}/; if( regex.test(txt) ){ var noArr = txt.split("-"); var year = eval(noArr[0]); var month = eval(noArr[1]); var day = eval(noArr[2]); if ( year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {return false;} if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;} if (month == 2){ if ((year % 4 != 0) && day > 29) { return false;} if (year % 4 == 0){ if(year % 100 == 0 && year % 400 != 0 && day > 29){return false;} else if (day > 28){ return false; } } } return false; } else{ return true; } } } //检测15位或18位的身份证号 function isIdCardNum(txt){ if(txt == null || txt == ""){ return false; } var idNo = txt.toString(); var len = idNo.length; var reg; var noArr; var dateStr=""; if(len != 15 && len != 18){ return false; } else if(len == 15){ reg = /(\d{3})(\d{3})(\d{2})(\d{2})(\d{2})(\d{3})/; if(reg.test(idNo)==false){ return false; } else{ noArr = reg.exec(idNo); dateStr = "19"+noArr[3]+"-"+noArr[4]+"-"+noArr[5]; return isDateFormat(dateStr); } } else{ reg = /(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})(\d{1})([0-9xX]{1})/; if(reg.test(idNo)==false){ return false; } else{ noArr = reg.exec(idNo); dateStr = noArr[7]+noArr[8]+noArr[9]+noArr[10]+"/"+noArr[11]+noArr[12]+"/"+noArr[13]+noArr[14]; if(isNaN(new Date(dateStr)) == false){ var wi = new Array(0,7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2); var amt = 0; var i; var chkNo = new Array("1","0","X","9","8","7","6","5","4","3","2"); for(i=1;i<18;i++){ amt += (noArr[i]*wi[i]); } return (chkNo[amt % 11] == noArr[18].toUpperCase()); } else{ return false; } } } }
标签:
原文地址:http://www.cnblogs.com/guwei4037/p/5592392.html