PeopleSoft-(中国)身份证格式化、校验函数

身份证格式化函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/******************************************************
Function : Format_NID
Purpose : Convert National_ID from National_ID_Format into National_ID
without special characters and viceversa
Version : 3.3.99
Parameters : COUNTRY,NID_TYPE,INPUT,DIRECTION
Returns : STRING
******************************************************/
Function Format_NID(&COUNTRY, &NID_TYPE, &INPUT, &DIRECTION) Returns string;
&ALPHA_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

If &COUNTRY = "HKG" Or
&COUNTRY = "MYS" Then
&ALPHA_STRING = &ALPHA_STRING | " ";
End-If;

/* permit DEU Pension-Insurance Number (MNRBV) to store some special chars. */
If &COUNTRY = "DEU" And
&NID_TYPE = "MNRBV" Then
&ALPHA_STRING = &ALPHA_STRING | " /-.";
End-If;


&NUM_STRING = "1234567890";
If &DIRECTION = "INTODB" Then
&OUTPUT = "";
If All(&INPUT) Then
&LENGTH = Len(&INPUT);
&LOOP = 1;
While &LOOP <= &LENGTH
/* check for alpha in the format */
If Find(Substring(&INPUT, &LOOP, 1), &ALPHA_STRING) <> 0 Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, 1);
Else
/* check for numeric in the format */
If Find(Substring(&INPUT, &LOOP, 1), &NUM_STRING) <> 0 Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, 1);
End-If;
End-If;
&LOOP = &LOOP + 1;
End-While;
End-If;
Return &OUTPUT;
Else
If All(&INPUT) Then
SQLExec("Select NATIONAL_ID_FORMAT from PS_NID_TYPE_TBL Where COUNTRY=:1 AND NATIONAL_ID_TYPE =:2", &COUNTRY, &NID_TYPE, &NATIONAL_ID_FORMAT);
&LENGTH = Len(&INPUT);
&FORMATLENGTH = Len(&NATIONAL_ID_FORMAT);
&LOOP = 1;
&STRINGCOUNTER = 1;

/* 12/27/99 [JDL] - Modified so that countries with 'NO' NID formatting display whatever is stored on the database. NO means "No Formatting" not "No NID display." */

If &NID_TYPE <> "NO" Then
While &LOOP <= &LENGTH Or
&STRINGCOUNTER <= &FORMATLENGTH
/* check for alpha in the format */
If Find(Substring(&NATIONAL_ID_FORMAT, &STRINGCOUNTER, 1), &ALPHA_STRING) <> 0 Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, 1);
Else
/* check for numeric in the format */
If Find(Substring(&NATIONAL_ID_FORMAT, &STRINGCOUNTER, 1), &NUM_STRING) <> 0 Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, 1);
/* check for special character in the format */
Else
If Substring(&NATIONAL_ID_FORMAT, &STRINGCOUNTER, 1) = Substring(&INPUT, &LOOP, 1) Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, 1);
Else
If &STRINGCOUNTER <= &FORMATLENGTH Then
&OUTPUT = &OUTPUT | Substring(&NATIONAL_ID_FORMAT, &STRINGCOUNTER, 1);
&LOOP = &LOOP - 1;
Else
If &FORMATLENGTH > 0 Then
&OUTPUT = &OUTPUT | Substring(&INPUT, &LOOP, Len(&INPUT));
End-If;
End-If;
End-If;
End-If;
End-If;
&LOOP = &LOOP + 1;
&STRINGCOUNTER = &STRINGCOUNTER + 1;
End-While;
If &LENGTH + 2 = &FORMATLENGTH And
&COUNTRY = "SGP" And
&NID_TYPE = "WP" Then
&OUTPUT = &OUTPUT | "-";
End-If;
Else
&OUTPUT = &INPUT;
End-If;
End-If;
/** in case an &INPUT exist, but no Format found ... **/
If None(&OUTPUT) Then
&OUTPUT = &INPUT
End-If;
Return &OUTPUT;
End-If;
End-Function;

身份证校验函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/******************************************************
Function : valid_NID 身份证号算法校验
Purpose : Convert National_ID from National_ID_Format into National_ID
without special characters and viceversa
Version : 3.3.99
Parameters : COUNTRY,NID_TYPE,INPUT
Returns : STRING
******************************************************/
Function valid_NID(&NID) Returns boolean;
/*身份证号(值与字段)*/
Local string &strNID = &NID;
Local string &strMonth, &strDay, &strIdBirth;
/*性别*/
Local string &strSex = " ";
Local number &numNIDSex, &i;
/*身份证号检验:权值和校验值*/
Local array of number &Wnum = CreateArray(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
Local array of string &Cstr = CreateArray("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"); /*校验值计算结果*/
Local number &numResult = 0;
If All(&strNID) Then
/*获取身份证号长度*/
&numNIDlen = Len(&strNID);

rem 验证省份;
SQLExec("select 'Y' from dual where :1 in ('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')", Substring(&strNID, 1, 2), &flag1);
If &flag1 <> "Y" Then
Return False;
End-If;

/*1. 检查生日是否有效*/
SQLExec("select 'Y' from dual where :1 in ('01','02','03','04','05','06','07','08','09','10','11','12')", Substring(&strNID, 11, 2), &flag2);
If &flag2 <> "Y" Then
Return False;
End-If;

If Substring(&strNID, 11, 2) = "01" Or
Substring(&strNID, 11, 2) = "03" Or
Substring(&strNID, 11, 2) = "05" Or
Substring(&strNID, 11, 2) = "07" Or
Substring(&strNID, 11, 2) = "08" Or
Substring(&strNID, 11, 2) = "10" Or
Substring(&strNID, 11, 2) = "12" Then
SQLExec(" select 'Y' from dual where to_number(substr(:1,13,2)) between 1 and 31", &strNID, &flag31);
If &flag31 <> "Y" Then
Return False;
End-If;
End-If;
If Substring(&strNID, 11, 2) = "04" Or
Substring(&strNID, 11, 2) = "06" Or
Substring(&strNID, 11, 2) = "09" Or
Substring(&strNID, 11, 2) = "11" Then
SQLExec(" select 'Y' from dual where to_number(substr(:1,13,2)) between 1 and 30", &strNID, &flag32);
If &flag32 <> "Y" Then
Return False;
End-If;
End-If;
If Substring(&strNID, 11, 2) = "02" Then
SQLExec("select MOD(to_number(:1), 4) from dual", Substring(&strNID, 7, 4), &num);
If &num = 0 Then
SQLExec(" select 'Y' from dual where to_number(substr(:1,13,2)) between 1 and 29", &strNID, &flag33);
If &flag33 <> "Y" Then
Return False;
End-If;
Else
SQLExec(" select 'Y' from dual where to_number(substr(:1,13,2)) between 1 and 28", &strNID, &flag34);
If &flag34 <> "Y" Then
Return False;
End-If;
End-If;
End-If;

SQLExec("select 'Y' from dual where to_date(substr(:1,7,8) ,'yyyymmdd') > to_date('19000101','yyyymmdd') and to_date(substr(:1,7,8) ,'yyyymmdd')<=sysdate", &strNID, &flag4);
If &flag4 <> "Y" Then
Return False;
End-If;
/*2. 校验性别是否与提供的性别一致(15位最后一位、18位第17位为性别)*/ /*15位最后一位、18位第17位为性别*/

/*3. 如果为18位身份证,校验最后一位校验位是否正确*/ /*=================================================================== 公式:∑(a[i]*W[i]) mod 11( i = 2, 3, ..., 18) "*" 表示乘号

i­­­­­­­­表示身份证号码每一位的序号,从右至左,最左侧为18,最右侧为1。 a[i]­­­­­表示身份证号码第 i 位上的号码 W[i]­­­­­表示第 i 位上的权值 W[i] = 2^(i­1) mod 11 权值列表: 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 以下是检验码表,R为计算结果,C为对应校验码: R 0 1 2 3 4 5 6 7 8 9 10 C 1 0 X 9 8 7 6 5 4 3 2 ===================================================================*/
If &numNIDlen = 18 Then
For &i = 1 To 17
&numResult = &numResult + &Wnum [&i] * Value(Substring(&strNID, &i, 1))
End-For;
&numResult = Mod(&numResult, 11);
If &Cstr [&numResult + 1] <> Right(&strNID, 1) Then

Return False;
End-If;
End-If;
End-If;
Return True;
End-Function;