VBScript and JavaScript version of the 15-bit, 18-bit identity card number validation function. And according to the ID card of the province, birthday, sex _vbs

Source: Internet
Author: User
Tags trim
Need to use identity card verification work, but also to support the examination of 15-bit and 18-bit.
I temporarily lazy, ask colleagues have no ready-made functions available, colleagues Google a bit, throw me a asp-vbscript version of the function.
But my side is the client JavaScript ah, so with the change Vbs-->js
After the change found good use, and found that VBScript really very long-winded, incredibly wrote more than 50 lines, I changed to JS only used 11 lines.
Of course, I do not mean VBScript, the writing of this section of the VBS code is also a Tang monk character ...

Give the VBScript code first,
Copy Code code as follows:

<script language= "VBScript" >

' Function: Check ID number
Function Checkcardid (E)
Arrverifycode = Split ("1,0,x,9,8,7,6,5,4,3,2", ",")
Wi = Split ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2", ",")
Checker = Split ("1,9,8,7,6,5,4,3,2,1,1", ",")

If Len (E) < or Len (e) = or Len (e) = or Len (e) > Then
Checkcardid= "ID number is 15 yards or 18 digits."
Checkcardid = False
Exit Function
End If

Dim Ai
If Len (e) = Then
Ai = Mid (E, 1, 17)
ElseIf Len (e) = Then
Ai = E
Ai = Left (AI, 6) & "A" & Mid (AI, 7, 9)
End If

If not IsNumeric (Ai) Then
Checkcardid= "ID card in addition to the last one, must be a number!" "
Exit Function
End If
Dim stryear, Strmonth, Strday
Stryear = CInt (Mid (Ai, 7, 4))
Strmonth = CInt (Mid (Ai, 11, 2))
Strday = CInt (Mid (Ai, 13, 2))
Birthday = Trim (stryear) + "-" + trim (strmonth) + "-" + trim (strday)
If IsDate (Birthday) Then
If DateDiff ("yyyy", now,birthday) <-140 or CDate (birthday) >date () Then
Checkcardid= "ID card input Error! "
Exit Function
End If
If strmonth > Or strday > Then
Checkcardid= "ID card input Error! "
Exit Function
End If
Else
Checkcardid= "ID card input Error! "
Exit Function
End If
Dim I, Totalmulaiwi
For i = 0 to 16
Totalmulaiwi = Totalmulaiwi + CInt (Mid (Ai, i + 1, 1)) * Wi (i)
Next
Dim Modvalue
Modvalue = Totalmulaiwi Mod 11
Dim Strverifycode
Strverifycode = Arrverifycode (modvalue)
ai = ai & Strverifycode
Checkcardid = Ai

If Len (e) = e <> Ai Then
Checkcardid= "ID card input Error! "
Exit Function
End If
End Function
</script>

Then release the JavaScript version code
Copy Code code as follows:

function Checkid (pId) {
Check the ID number Go_rush (Asun) from http://ashun.cnblogs.com

var arrverifycode = [1,0, "X", 9,8,7,6,5,4,3,2];
var Wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
var Checker = [1,9,8,7,6,5,4,3,2,1,1];

if (pid.length!= && pid.length!=) return "The ID number has 15 yards or 18 digits";

var ai=pid.length==18? Pid.substring (0,17): Pid.slice (0,6) + "+pid.slice" (6,16);

if (!/^\d+$/.test (Ai)) return "ID card except the last one, must be a number!" ";

var yyyy=ai.slice (6,10), Mm=ai.slice (10,12)-1, Dd=ai.slice (12,14);

var d=new date (YYYY,MM,DD), now=new date ();
var year=d.getfullyear (), Mon=d.getmonth (), day=d.getdate ();

if (year!=yyyy | | mon!=mm | | day!=dd | | d>now | | year<1940) return "ID card input Error! ";

for (Var i=0,ret=0;i<17;i++) Ret+=ai.charat (i) *wi[i];
Ai+=arrverifycode[ret%=11];

return pid.length ==18 && pId!= Ai? " ID Card input Error! ": Ai;
};

As for the principle of the ID card algorithm, please refer to: http://www.google.com/

Finally, according to the ID number of the province, birthday, sex is very simple
Copy Code code as follows:

<script language= "JavaScript" >
var id= "342201570202003"

According to the ID card of the province, birthday, gender Go_rush (Asun) from http://ashun.cnblogs.com/
function GetInfo (ID) {
var arr=[null,null,null,null,null,null,null,null,null,null,null, "Beijing", "Tianjin", "Hebei", "Shanxi", "Inner Mongolia"
, Null,null,null,null,null, "Liaoning", "Jilin", "Heilongjiang", Null,null,null,null,null,null,null, "Shanghai"
, "Jiangsu", "Zhejiang", "Ann Micro", "Fujian", "Jiangxi", "Shandong", Null,null,null, "Henan", "Hubei", "Hunan", "Guangdong", "Guangxi", "Hainan"
, Null,null,null, "Chongqing", "Sichuan", "Guizhou", "Yunnan", "Tibet", Null,null,null,null,null,null, "Shaanxi", "Gansu"
, "Qinghai", "Ningxia", "Xinjiang", Null,null,null,null,null, "Taiwan", Null,null,null,null,null,null,null,null
, NULL, "Hong Kong", "Macao", Null,null,null,null,null,null,null,null, "foreign"]
Id=checkid (ID)
if (isNaN (ID)) return "Wrong ID number"
var id=string (id), Prov=arr[id.slice (0,2)], Sex=id.slice (14,17)%2? "Male": "Female"
var birthday= (New Date (Id.slice (6,10), Id.slice (10,12)-1, Id.slice (12,14)). toLocaleDateString ()
return [Prov,birthday,sex]
}

Alert (GetInfo (ID))
</script>

The above code can only be taken to the province, to take to the specific city, please see my other essay,
The code only took me and a few colleagues of the ID number measured, if there is a bug, please point out

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.