1. 程式人生 > 其它 >正則驗證身份證、數字、數字字母

正則驗證身份證、數字、數字字母

// 對不是必填屬性的驗證(手機號的驗證)
export const matchPhone = (control: FormControl): { [key: string]: boolean } => {
if (control.value == null || '' === control.value) {
return null;
}
const tell = control.value;
if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(tell))) {
return {nomatch: true};
} else {
return null;
// 這裡一定是null
}
};
// 身份證正則校驗
export const matchSfzh = (control: FormControl): { [key: string]: boolean } => {
const sfzh = control.value;
if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(sfzh))) {
return {nomatch: true};
} else {
return null;
// 這裡一定是null
}
};
// 數字正則校驗
export const matchNumber = (control: FormControl): { [key: string]: boolean } => {
if (control.value == null || '' === control.value) {
return null;
}
const ex = control.value;
if (!(/^[0-9]*$/.test(ex))) {
return {nomatch: true};
} else {
return null;
// 這裡一定是null
}
};
// 英文字母校驗
export const matchWord = (control: FormControl): { [key: string]: boolean } => {
if (control.value == null || '' === control.value) {
return null;
}
const ex = control.value;
if (!(/^[A-Za-z]+$/.test(ex))) {
return {nomatch: true};
} else {
return null; // 這裡一定是null
}
};
// 英文數字組合正則校驗
export const matchNumberAndWord = (control: FormControl): { [key: string]: boolean } => {
if (control.value == null || '' === control.value) {
return null;
}
const ex = control.value;
if (!(/^[A-Za-z0-9]+$/.test(ex))) {
return {nomatch: true};
} else {
return null; // 這裡一定是null
}
};