Angular 南非身份证号码验证
我对此进行了研究,但我使用的代码似乎都不起作用。南非身份证号码包含出生日期和性别。我所希望的只是当他们的ID号被输入到一个输入字段中时,以角度输入该信息并进行验证 我尝试过使用javascript代码,目前我刚刚将其修改为typescript。我没有收到任何错误,但它根本没有验证Angular 南非身份证号码验证,angular,Angular,我对此进行了研究,但我使用的代码似乎都不起作用。南非身份证号码包含出生日期和性别。我所希望的只是当他们的ID号被输入到一个输入字段中时,以角度输入该信息并进行验证 我尝试过使用javascript代码,目前我刚刚将其修改为typescript。我没有收到任何错误,但它根本没有验证 ts this.registerForm = this.formBuilder.group({ confirm: ['', [Validators.required, Validators.email]],
ts
this.registerForm = this.formBuilder.group({
confirm: ['', [Validators.required, Validators.email]],
};
validateRSAidnumber(idnumber) {
console.log('idnumber', idnumber);
let invalid = 0;
// check that value submitted is a number
if (isNaN(idnumber)) {
invalid++;
}
// check length of 13 digits
if (idnumber.length !== 13) {
invalid++;
}
// check that YYMMDD group is a valid date
const yy = idnumber.substring(0, 2);
const mm = idnumber.substring(2, 4);
const dd = idnumber.substring(4, 6);
const dob = new Date(yy, (mm - 1), dd);
// check values - add one to month because Date() uses 0-11 for months
if (!(((dob.getFullYear() + '').substring(2, 4) === yy) && (dob.getMonth() === mm - 1) && (dob.getDate() === dd))) {
invalid++;
}
// evaluate GSSS group for gender and sequence
const gender = parseInt(idnumber.substring(6, 10), 10) > 5000 ? 'M' : 'F';
// ensure third to last digit is a 1 or a 0
if (idnumber.substring(10, 11) > 1) {
invalid++;
}
// ensure second to last digit is a 8 or a 9
if (idnumber.substring(11, 12) < 8) {
invalid++;
}
// calculate check bit (Z) using the Luhn algorithm
let ncheck = 0;
let beven = false;
for (let c = idnumber.length - 1; c >= 0; c--) {
const cdigit = idnumber.charAt(c);
let ndigit = parseInt(cdigit, 10);
if (beven) {
if ((ndigit *= 2) > 9) ndigit -= 9;
}
ncheck += ndigit;
beven = !beven;
}
if ((ncheck % 10) !== 0) {
invalid++;
}
return !invalid;
}
// convenience getter for easy access to form fields
get f() { return this.registerForm.controls; }
get isEmailMismatch() { return this.registerForm.getError('emailMismatch'); }
onSubmit() {
console.log('buttoj');
this.submitted = true;
this.userService.user.user.email = this.email;
this.userService.user.user.first_name = this.firstName;
this.userService.user.user.last_name = this.lastName;
this.userService.user.user.id_number = this.idNumber;
this.userService.user.user.password = this.password;
this.userService.user.user.phone = '0' + this.contactNumber.toString();
this.userService.user.user.id_number = this.idNumber.toString();
this.registerUser();
this.validateRSAidnumber(this.idNumber);
}
ts
this.registerForm=this.formBuilder.group({
确认:[''[Validators.required,Validators.email]],
};
validateRSAidnumber(idnumber){
console.log('idnumber',idnumber);
设无效=0;
//检查提交的值是否为数字
if(isNaN(idnumber)){
无效的++;
}
//检查13位数字的长度
if(idnumber.length!==13){
无效的++;
}
//检查YYMMDD组是否为有效日期
常数yy=idnumber.substring(0,2);
常量mm=idnumber.子字符串(2,4);
constdd=idnumber.substring(4,6);
const dob=新日期(年月日);
//检查值-向月份添加一个,因为Date()对月份使用0-11
如果(!(((dob.getFullYear()+“”).substring(2,4)==yy)和&(dob.getMonth()==mm-1)和&(dob.getDate()==dd))){
无效的++;
}
//评估GSSS组的性别和顺序
const-gender=parseInt(idnumber.substring(6,10),10)>5000?'M':'F';
//确保从第三位到最后一位数字为1或0
if(idnumber.子串(10,11)>1){
无效的++;
}
//确保倒数第二位数字为8或9
if(idnumber.substring(11,12)<8){
无效的++;
}
//使用Luhn算法计算校验位(Z)
设ncheck=0;
让贝文=假;
for(设c=idnumber.length-1;c>=0;c--){
常数cdigit=idnumber.charAt(c);
设ndigit=parseInt(cdigit,10);
如果(贝文){
如果((ndigit*=2)>9)ndigit-=9;
}
ncheck+=ndigit;
贝文=!贝文;
}
如果((n检查%10)!==0){
无效的++;
}
返回!无效;
}
//方便的getter,便于访问表单字段
get f(){返回this.registerForm.controls;}
get-isEmailMismatch(){返回this.registerForm.getError('emailMismatch');}
onSubmit(){
console.log('buttj');
this.submitted=true;
this.userService.user.user.email=this.email;
this.userService.user.user.first_name=this.firstName;
this.userService.user.user.last_name=this.lastName;
this.userService.user.user.id\u number=this.idNumber;
this.userService.user.user.password=this.password;
this.userService.user.user.phone='0'+this.contactNumber.toString();
this.userService.user.user.id_number=this.idNumber.toString();
这是registerUser();
this.validateRSAidnumber(this.idNumber);
}
南非身份证号码(例如880123511088)中存储的信息:
- 数字1和2=出生年份(例如1988年)
- 数字3和4=出生月份(例如1月1日)
- 第5位和第6位=出生日期(例如第23天)
- 数字7到10用于定义性别(女性=0到4999;男性=5000到9999)(例如5111=男性)
- 数字11用于对您的公民身份进行分类(SA公民=0;非SA公民=1)
- 直到20世纪80年代,数字12才被用来对种族进行分类
- 最后一位数字13用作校验和,以确定ID号是否有效
Id = str(input("Please enter your South African Identity Number(ID): "))
#Id = "8801235111088"
#Date of birth
year = int(Id[0:2])
month = Id[2:4]
day = Id[4:6]
if year >= 50:
print("Date of birth is: ", day, month, "19" + str(year) )
if year < 50:
print("Date of birth is: " , day, month, "20" + str(year))
#gender
gender = int(Id[6:10])
if 0000 <= gender <= 4999:
print("Gender is : female")
if 5000 <= gender <= 9999:
print("Gender is: male")
#Citizen status
citizen = int(Id[10])
if citizen == 0:
print("You are a South African citizen")
if citizen == 1:
print("You are not a South African citizen")
Id=str(输入(“请输入您的南非身份证号码(Id):”)
#Id=“880123511088”
#出生日期
年份=整数(Id[0:2])
月份=Id[2:4]
日期=Id[4:6]
如果年份>=50:
打印(“出生日期为:”,日,月,“19”+str(年))
如果年份<50:
打印(“出生日期为:”,日,月,“20”+str(年))
#性别
性别=整数(Id[6:10])
如果0000南非ID号中存储的信息(例如880123511088):
- 数字1和2=出生年份(例如1988年)
- 数字3和4=出生月份(例如1月1日)
- 第5位和第6位=出生日期(例如第23天)
- 数字7到10用于定义性别(女性=0到4999;男性=5000到9999)(例如5111=男性)
- 数字11用于对您的公民身份进行分类(SA公民=0;非SA公民=1)
- 直到20世纪80年代,数字12才被用来对种族进行分类
- 最后一位数字13用作校验和,以确定ID号是否有效
以下是从任何南非ID号提取信息的Python脚本:
Id = str(input("Please enter your South African Identity Number(ID): "))
#Id = "8801235111088"
#Date of birth
year = int(Id[0:2])
month = Id[2:4]
day = Id[4:6]
if year >= 50:
print("Date of birth is: ", day, month, "19" + str(year) )
if year < 50:
print("Date of birth is: " , day, month, "20" + str(year))
#gender
gender = int(Id[6:10])
if 0000 <= gender <= 4999:
print("Gender is : female")
if 5000 <= gender <= 9999:
print("Gender is: male")
#Citizen status
citizen = int(Id[10])
if citizen == 0:
print("You are a South African citizen")
if citizen == 1:
print("You are not a South African citizen")
Id=str(输入(“请输入您的南非身份证号码(Id):”)
#Id=“880123511088”
#出生日期
年份=整数(Id[0:2])
月份=Id[2:4]
日期=Id[4:6]
如果年份>=50:
打印(“出生日期为:”,日,月,“19”+str(年))
如果年份<50:
打印(“出生日期:”,日,月,“20”+str(年))
#性别
性别=整数(Id[6:10])
如果0000,请添加一些示例数据。我有一个简单的输入文件,我正在尝试验证angularthis.RegisterPerform=this.formBuilder.group({idNumber:['',Validators.required],email:['',[Validators.required,Validators.email]],确认:['',[Validators.required,Validators.email]],联系人号码:['',Validators.required],密码:['',[Validators.required,Validators.minLength(6)],确认密码:['',Validators.required]},{validator:(表单:FormGroup)=>form.get('email')。value!==form.get('confirm')。value?{emailmissmatch:true}:null,},我对angular很陌生,所以我想弄明白这一点out@thabangNkosi请不要使用