最好js代码验证×××号码
生活随笔
收集整理的這篇文章主要介紹了
最好js代码验证×××号码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文轉(zhuǎn)載于(請(qǐng)務(wù)必注明作者出處): http://www.blogjava.net/titanaly/archive/2011/08/25/357268.html 1. 自定義js類如下: // 構(gòu)造函數(shù),變量為15位或者18位的×××號(hào)碼
function clsIDCard(CardNo) {
??this.Valid = false;
??this.ID15 = '';
??this.ID18 = '';
??this.Local = '';
??if (CardNo != null)
????this.SetCardNo(CardNo);
}
// 設(shè)置×××號(hào)碼,15位或者18位
clsIDCard.prototype.SetCardNo = function(CardNo) {
??this.ID15 = '';
??this.ID18 = '';
??this.Local = '';
??CardNo = CardNo.replace(" ", "");
??var strCardNo;
??if (CardNo.length == 18) {
????pattern = /^\d{17}(\d|x|X)$/;
????if (pattern.exec(CardNo) == null)
??????return;
????strCardNo = CardNo.toUpperCase();
??} else {
????pattern = /^\d{15}$/;
????if (pattern.exec(CardNo) == null)
??????return;
????strCardNo = CardNo.substr(0, 6) + '19' + CardNo.substr(6, 9)
????strCardNo += this.GetVCode(strCardNo);
??}
??this.Valid = this.CheckValid(strCardNo);
}
// 校驗(yàn)×××有效性
clsIDCard.prototype.IsValid = function() {
??return this.Valid;
}
// 返回生日字符串,格式如下,1981-10-10
clsIDCard.prototype.GetBirthDate = function() {
??var BirthDate = '';
??if (this.Valid)
????BirthDate = this.GetBirthYear() + '-' + this.GetBirthMonth() + '-'
????????+ this.GetBirthDay();
??return BirthDate;
}
// 返回生日中的年,格式如下,1981
clsIDCard.prototype.GetBirthYear = function() {
??var BirthYear = '';
??if (this.Valid)
????BirthYear = this.ID18.substr(6, 4);
??return BirthYear;
}
// 返回生日中的月,格式如下,10
clsIDCard.prototype.GetBirthMonth = function() {
??var BirthMonth = '';
??if (this.Valid)
????BirthMonth = this.ID18.substr(10, 2);
??if (BirthMonth.charAt(0) == '0')
????BirthMonth = BirthMonth.charAt(1);
??return BirthMonth;
}
// 返回生日中的日,格式如下,10
clsIDCard.prototype.GetBirthDay = function() {
??var BirthDay = '';
??if (this.Valid)
????BirthDay = this.ID18.substr(12, 2);
??return BirthDay;
}
// 返回性別,1:男,0:女
clsIDCard.prototype.GetSex = function() {
??var Sex = '';
??if (this.Valid)
????Sex = this.ID18.charAt(16) % 2;
??return Sex;
}
// 返回15位×××號(hào)碼
clsIDCard.prototype.Get15 = function() {
??var ID15 = '';
??if (this.Valid)
????ID15 = this.ID15;
??return ID15;
}
// 返回18位×××號(hào)碼
clsIDCard.prototype.Get18 = function() {
??var ID18 = '';
??if (this.Valid)
????ID18 = this.ID18;
??return ID18;
}
// 返回所在省,例如:上海市、浙江省
clsIDCard.prototype.GetLocal = function() {
??var Local = '';
??if (this.Valid)
????Local = this.Local;
??return Local;
}
clsIDCard.prototype.GetVCode = function(CardNo17) {
??var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
??var Ai = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
??var cardNoSum = 0;
??for (var i = 0; i < CardNo17.length; i++)
????cardNoSum += CardNo17.charAt(i) * Wi[i];
??var seq = cardNoSum % 11;
??return Ai[seq];
}
clsIDCard.prototype.CheckValid = function(CardNo18) {
??if (this.GetVCode(CardNo18.substr(0, 17)) != CardNo18.charAt(17))
????return false;
??if (!this.IsDate(CardNo18.substr(6, 8)))
????return false;
??var aCity = {
????11 : "北京",
????12 : "天津",
????13 : "河北",
????14 : "山西",
????15 : "內(nèi)蒙古",
????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 : "臺(tái)灣",
????81 : "香港",
????82 : "澳門(mén)",
????91 : "國(guó)外"
??};
??if (aCity[parseInt(CardNo18.substr(0, 2))] == null)
????return false;
??this.ID18 = CardNo18;
??this.ID15 = CardNo18.substr(0, 6) + CardNo18.substr(8, 9);
??this.Local = aCity[parseInt(CardNo18.substr(0, 2))];
??return true;
}
clsIDCard.prototype.IsDate = function(strDate) {
??var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
??if (r == null)
????return false;
??var d = new Date(r[1], r[2] - 1, r[3]);
??return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d
??????.getDate() == r[3]);
} 2. 頁(yè)面只需要new出對(duì)象,并傳遞數(shù)據(jù)驗(yàn)證,并可獲得相關(guān)數(shù)據(jù)(?住址 | 出生日期 | 性別?)即可: $("#cardNo").blur(function(event){
????var idCard = $(this).val();
????
????var checkFlag = new clsIDCard(idCard);????
????if( !checkFlag.IsValid() ){
??????alert("×××錯(cuò)誤");
??????return false;
????}else{
??????alert( "出生于: " + checkFlag.GetBirthDate() +" 地區(qū):" + checkFlag.GetLocal() +" sex:" + checkFlag.GetSex() );
????}????
??});
function clsIDCard(CardNo) {
??this.Valid = false;
??this.ID15 = '';
??this.ID18 = '';
??this.Local = '';
??if (CardNo != null)
????this.SetCardNo(CardNo);
}
// 設(shè)置×××號(hào)碼,15位或者18位
clsIDCard.prototype.SetCardNo = function(CardNo) {
??this.ID15 = '';
??this.ID18 = '';
??this.Local = '';
??CardNo = CardNo.replace(" ", "");
??var strCardNo;
??if (CardNo.length == 18) {
????pattern = /^\d{17}(\d|x|X)$/;
????if (pattern.exec(CardNo) == null)
??????return;
????strCardNo = CardNo.toUpperCase();
??} else {
????pattern = /^\d{15}$/;
????if (pattern.exec(CardNo) == null)
??????return;
????strCardNo = CardNo.substr(0, 6) + '19' + CardNo.substr(6, 9)
????strCardNo += this.GetVCode(strCardNo);
??}
??this.Valid = this.CheckValid(strCardNo);
}
// 校驗(yàn)×××有效性
clsIDCard.prototype.IsValid = function() {
??return this.Valid;
}
// 返回生日字符串,格式如下,1981-10-10
clsIDCard.prototype.GetBirthDate = function() {
??var BirthDate = '';
??if (this.Valid)
????BirthDate = this.GetBirthYear() + '-' + this.GetBirthMonth() + '-'
????????+ this.GetBirthDay();
??return BirthDate;
}
// 返回生日中的年,格式如下,1981
clsIDCard.prototype.GetBirthYear = function() {
??var BirthYear = '';
??if (this.Valid)
????BirthYear = this.ID18.substr(6, 4);
??return BirthYear;
}
// 返回生日中的月,格式如下,10
clsIDCard.prototype.GetBirthMonth = function() {
??var BirthMonth = '';
??if (this.Valid)
????BirthMonth = this.ID18.substr(10, 2);
??if (BirthMonth.charAt(0) == '0')
????BirthMonth = BirthMonth.charAt(1);
??return BirthMonth;
}
// 返回生日中的日,格式如下,10
clsIDCard.prototype.GetBirthDay = function() {
??var BirthDay = '';
??if (this.Valid)
????BirthDay = this.ID18.substr(12, 2);
??return BirthDay;
}
// 返回性別,1:男,0:女
clsIDCard.prototype.GetSex = function() {
??var Sex = '';
??if (this.Valid)
????Sex = this.ID18.charAt(16) % 2;
??return Sex;
}
// 返回15位×××號(hào)碼
clsIDCard.prototype.Get15 = function() {
??var ID15 = '';
??if (this.Valid)
????ID15 = this.ID15;
??return ID15;
}
// 返回18位×××號(hào)碼
clsIDCard.prototype.Get18 = function() {
??var ID18 = '';
??if (this.Valid)
????ID18 = this.ID18;
??return ID18;
}
// 返回所在省,例如:上海市、浙江省
clsIDCard.prototype.GetLocal = function() {
??var Local = '';
??if (this.Valid)
????Local = this.Local;
??return Local;
}
clsIDCard.prototype.GetVCode = function(CardNo17) {
??var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
??var Ai = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
??var cardNoSum = 0;
??for (var i = 0; i < CardNo17.length; i++)
????cardNoSum += CardNo17.charAt(i) * Wi[i];
??var seq = cardNoSum % 11;
??return Ai[seq];
}
clsIDCard.prototype.CheckValid = function(CardNo18) {
??if (this.GetVCode(CardNo18.substr(0, 17)) != CardNo18.charAt(17))
????return false;
??if (!this.IsDate(CardNo18.substr(6, 8)))
????return false;
??var aCity = {
????11 : "北京",
????12 : "天津",
????13 : "河北",
????14 : "山西",
????15 : "內(nèi)蒙古",
????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 : "臺(tái)灣",
????81 : "香港",
????82 : "澳門(mén)",
????91 : "國(guó)外"
??};
??if (aCity[parseInt(CardNo18.substr(0, 2))] == null)
????return false;
??this.ID18 = CardNo18;
??this.ID15 = CardNo18.substr(0, 6) + CardNo18.substr(8, 9);
??this.Local = aCity[parseInt(CardNo18.substr(0, 2))];
??return true;
}
clsIDCard.prototype.IsDate = function(strDate) {
??var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
??if (r == null)
????return false;
??var d = new Date(r[1], r[2] - 1, r[3]);
??return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d
??????.getDate() == r[3]);
} 2. 頁(yè)面只需要new出對(duì)象,并傳遞數(shù)據(jù)驗(yàn)證,并可獲得相關(guān)數(shù)據(jù)(?住址 | 出生日期 | 性別?)即可: $("#cardNo").blur(function(event){
????var idCard = $(this).val();
????
????var checkFlag = new clsIDCard(idCard);????
????if( !checkFlag.IsValid() ){
??????alert("×××錯(cuò)誤");
??????return false;
????}else{
??????alert( "出生于: " + checkFlag.GetBirthDate() +" 地區(qū):" + checkFlag.GetLocal() +" sex:" + checkFlag.GetSex() );
????}????
??});
轉(zhuǎn)載于:https://blog.51cto.com/kinglixing/976883
總結(jié)
以上是生活随笔為你收集整理的最好js代码验证×××号码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 图像处理【代码合集】
- 下一篇: ubuntu8.04安装java+ecl