求一个校验身份证号码有效性的代码
可以根据一个输入的身份证号码,校验身份证号码中的出生年月日的有效性和末位的有效性。
[解决办法]
Function sf
Lparameters lcString
If Len(Alltrim(lcString)) != 18
Return 0
Endif
Dimension quan(17)
quan(1)=7
quan(2)=9
quan(3)=10
quan(4)=5
quan(5)=8
quan(6)=4
quan(7)=2
quan(8)=1
quan(9)=6
quan(10)=3
quan(11)=7
quan(12)=9
quan(13)=10
quan(14)=5
quan(15)=8
quan(16)=4
quan(17)=2
sn = 0
For i = 1 To 17
sn = sn + Val(Substr(lcString,i,1)) * quan(i)
Next
lnTemp = Int(Mod(sn,11)) &&计算最后一个验证码
lcYan = 'a'
If lnTemp == 0
lcYan = '1'
Else
If lnTemp == 1
lcYan = '0'
Else
If lnTemp = 2
lcYan = 'X'
Else
lcYan = Transform(12 - lnTemp)
Endif
Endif
Endif
If Right(lcString,1) != lcYan
Return 0
Endif
If !Left(lcString,2) $ '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 '
Return 0
Endif
If Val(Substr(lcString,7,4)) <= 1950
Return 0
Endif
If Year(Date()) - Val(Substr(lcString,7,4)) < 15
Return 0
Endif
If Val(Substr(lcString,11,2)) < 1 Or Val(Substr(lcString,11,2)) > 12
Return 0
Endif
If Val(Substr(lcString,13,2)) < 0 Or Val(Substr(lcString,13,2)) > 31
Return 0
Endif
Return 1
Endfunc
SFZH=[141027197802163033]
*-- 判断身份证长度
IF LEN(SFZH)!=18
MESSAGEBOX([身份证号码长度为 ]+TRANSFORM(LEN(SFZH))+[ 位])
ENDIF
*-- 判断年
NYEAR=INT(VAL(SUBSTR(SFZH,7,4)))
IF NOT BETWEEN(NYEAR,1900,YEAR(DATE()))
MESSAGEBOX([身份证 年 错误 【]+TRANSFORM(NYEAR)+[】])
RETURN NYEAR
ELSE
*-- 判断月
NMONTH=INT(VAL(SUBSTR(SFZH,11,2)))
IF NOT BETWEEN(NMONTH,1,12)
MESSAGEBOX([身份证 月 错误 【]+TRANSFORM(NMONTH)+[】])
RETURN NMONTH
ELSE
*-- 判断日
NDAY=INT(VAL(SUBSTR(SFZH,13,2)))
RQ=DATE(NYEAR,NMONTH,1)
IF NOT BETWEEN(NDAY,1,GOMONTH(RQ,1)-RQ)
MESSAGEBOX([身份证 日 错误 【]+TRANSFORM(NDAY)+[】 本月只有 ]+TRANSFORM(GOMONTH(RQ,1)-RQ)+[ 天])
RETURN NDAY
ENDIF
ENDIF
ENDIF
*-- 获取校验码
X=[7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2]
NUM=0
FOR I=1 TO 17
NUM=NUM+INT(VAL(SUBSTR(SFZH,I,1)))*INT(VAL(GETWORDNUM(X,I,[ ])))
ENDFOR
* Y值: 0 1 2 3 4 5 6 7 8 9 10
*校验码: 1 0 X 9 8 7 6 5 4 3 2
XYM=[1 0 X 9 8 7 6 5 4 3 2]
CXYM=GETWORDNUM(XYM,MOD(NUM,11)+1,[ ]) &&提取的第18位校验码
MESSAGEBOX([生成的第18位数字为【]+CXYM+[】]+IIF(SUBSTR(SFZH,18,1)==CXYM,[与身份证相符],[与身份证不相符])+[【]+SUBSTR(SFZH,18,1)+[】])