JQuery学习(2):验证脚本
1 //邮箱验证 2 function EmailVerify(str) { 3 var Regex = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/; 4 return Regex.test(str) 5 } 6 //座机验证 7 function PhoneVerify(str) { 8 var Regex = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/; 9 return Regex.test(str); 10 } 11 //邮编验证 12 function CodeVerify(str) { 13 var Regex = /^[0-9]{6}$/; 14 return Regex.test(str); 15 } 16 //URL验证 17 function UrlVerify(str) { 18 var Regex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/; 19 return Regex.test(str); 20 } 21 //IP验证 22 function IpVerify(str) { 23 var Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; 24 return Regex.test(str); 25 } 26 //HTML验证 27 function HtmlVerify(str) { 28 var Regex = /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/; 29 return Regex.test(str); 30 } 31 //Unicode编码中的汉字范围验证 32 function UnicodeVerify(str) { 33 var Regex = /^[\u2E80-\u9FFF]+$/; 34 return Regex.test(str); 35 } 36 //*****************身份证验证*****************// 37 function IdcardVerify(str) { 38 var allowancePersonValue = str; 39 //是否为空 40 if (allowancePersonValue == "") { 41 alert("输入不能为空!"); 42 return false; 43 } 44 //校验长度,类型 45 else if (isCardNo(allowancePersonValue) == false) { 46 alert("您输入的身份证号码格式不正确,请重新输入"); 47 return false; 48 } 49 //检查省份 50 else if (checkProvince(allowancePersonValue) == false) { 51 alert("您输入的身份证号码省份不正确,请重新输入"); 52 return false; 53 } 54 //校验生日 55 else if (checkBirthday(allowancePersonValue) == false) { 56 alert("您输入的身份证号码生日不正确,请重新输入"); 57 return false; 58 } else { 59 alert("验证通过"); 60 } 61 } 62 //身份证省的编码 63 var vcity = { 64 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 65 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 66 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 67 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 68 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 69 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" 70 }; 71 //检查号码是否符合规范,包括长度,类型 72 function isCardNo(card) { 73 //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X 74 var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 75 if (reg.test(card) == false) { 76 return false; 77 } 78 return true; 79 } 80 //取身份证前两位,校验省份 81 function checkProvince(card) { 82 var province = card.substr(0, 2); 83 if (vcity[province] == undefined) { 84 return false; 85 } 86 return true; 87 } 88 //检查生日是否正确 89 function checkBirthday(card) { 90 var len = card.length; 91 //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字 92 if (len == ‘15‘) { 93 var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; 94 var arr_data = card.match(re_fifteen); 95 var year = arr_data[2]; 96 var month = arr_data[3]; 97 var day = arr_data[4]; 98 var birthday = new Date(‘19‘ + year + ‘/‘ + month + ‘/‘ + day); 99 return verifyBirthday(‘19‘ + year, month, day, birthday); 100 } 101 //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X 102 if (len == ‘18‘) { 103 var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/; 104 var arr_data = card.match(re_eighteen); 105 var year = arr_data[2]; 106 var month = arr_data[3]; 107 var day = arr_data[4]; 108 var birthday = new Date(year + ‘/‘ + month + ‘/‘ + day); 109 return verifyBirthday(year, month, day, birthday); 110 } 111 return false; 112 } 113 //校验日期 114 function verifyBirthday(year, month, day, birthday) { 115 var now = new Date(); 116 var now_year = now.getFullYear(); 117 //年月日是否合理 118 if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) { 119 //判断年份的范围(3岁到100岁之间) 120 var time = now_year - year; 121 if (time >= 3 && time <= 120) { 122 return true; 123 } 124 return false; 125 } 126 return false; 127 } 128 //*****************身份证验证*****************//
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。