You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
5.9 KiB
249 lines
5.9 KiB
2 years ago
|
export class SimpleRequired {
|
||
|
required = true;
|
||
|
message: string;
|
||
|
trigger = 'blur';
|
||
|
|
||
|
constructor(name?: string) {
|
||
|
if (name) {
|
||
|
this.message = '请输入' + name;
|
||
|
} else {
|
||
|
this.message = '此处不能为空';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class SimpleCharacter {
|
||
|
required: boolean;
|
||
|
message: string;
|
||
|
max?: number;
|
||
|
trigger: string = 'blur';
|
||
|
|
||
|
constructor(name?: string, max?: number, required?: boolean) {
|
||
|
if (required == null || required == undefined) {
|
||
|
this.required = true;
|
||
|
} else {
|
||
|
this.required = required;
|
||
|
}
|
||
|
if (!max) {
|
||
|
max = 255;
|
||
|
}
|
||
|
this.max = max;
|
||
|
if (name) {
|
||
|
if (this.required) {
|
||
|
this.message = '请输入不超过' + max + '字符的' + name;
|
||
|
} else {
|
||
|
this.message = name + '最长不超过' + max + '字符';
|
||
|
}
|
||
|
} else {
|
||
|
this.message = '此处不能为空';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Character {
|
||
|
pattern: RegExp;
|
||
|
message: string = '输入格式不正确';
|
||
|
trigger = 'blur';
|
||
|
|
||
|
constructor(pattern: RegExp) {
|
||
|
this.pattern = pattern;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class SimpleSelection {
|
||
|
required: boolean;
|
||
|
message: string;
|
||
|
trigger: string;
|
||
|
|
||
|
constructor(name?: string) {
|
||
|
this.required = true;
|
||
|
this.trigger = 'change';
|
||
|
if (name) {
|
||
|
this.message = '请选择' + name;
|
||
|
} else {
|
||
|
this.message = '此处不能为空';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class SimpleNumber {
|
||
|
trigger: string = 'blur';
|
||
|
min?: Number;
|
||
|
max?: Number;
|
||
|
validator = (rule: any, value: any, callback: any) => {
|
||
|
if (value == null || value === '') {
|
||
|
callback();
|
||
|
}
|
||
|
if (!new RegExp(/^(0|-?[1-9]\d*)$/).test(value)) {
|
||
|
callback(new Error('请输入整数'));
|
||
|
}
|
||
|
const num = parseInt(value);
|
||
|
if (rule.min != null && num < rule.min) {
|
||
|
callback(new Error('最小值为' + rule.min));
|
||
|
}
|
||
|
if (rule.max != null && num > rule.max) {
|
||
|
callback(new Error('最大值为' + rule.max));
|
||
|
}
|
||
|
callback();
|
||
|
};
|
||
|
|
||
|
constructor(min?: number, max?: Number) {
|
||
|
this.min = min;
|
||
|
this.max = max;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Hexadecimal {
|
||
|
trigger = 'blur';
|
||
|
min?: Number;
|
||
|
max?: Number;
|
||
|
validator = (rule: any, value: any, callback: any) => {
|
||
|
if (value == null) {
|
||
|
callback();
|
||
|
} else if (
|
||
|
value.trim().length == 0 ||
|
||
|
!new RegExp(/^[0-9a-fA-F\s]+$/).test(value)
|
||
|
) {
|
||
|
callback(new Error('请输入十六进制数字'));
|
||
|
} else if (rule.min != null && value.length < rule.min) {
|
||
|
callback(new Error('请输入至少' + rule.min + '位'));
|
||
|
} else if (rule.max != null && value.length > rule.max) {
|
||
|
callback(new Error('请输入最多' + rule.max + '位'));
|
||
|
} else {
|
||
|
callback();
|
||
|
}
|
||
|
};
|
||
|
constructor(min?: number, max?: Number) {
|
||
|
this.min = min;
|
||
|
this.max = max;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Longitude extends Character {
|
||
|
constructor() {
|
||
|
super(
|
||
|
new RegExp(
|
||
|
/^-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,6})?)|180(([.][0]{1,6})?))$/
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
export class Latitude extends Character {
|
||
|
constructor() {
|
||
|
super(
|
||
|
new RegExp(
|
||
|
/^-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,6})?)|180(([.][0]{1,6})?))$/
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Email extends Character {
|
||
|
constructor() {
|
||
|
super(
|
||
|
new RegExp(
|
||
|
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
export class Phone extends Character {
|
||
|
constructor() {
|
||
|
super(
|
||
|
new RegExp(
|
||
|
/^(0|86|17951)?(13[0-9]|15[012356789]|17[0135678]|18[0-9]|14[57])[0-9]{8}$/
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Ipv4 extends Character {
|
||
|
constructor() {
|
||
|
super(new RegExp(/^((25[0-5]|2[0-4]\d|[01]?\d\d?)($|(?!\.$)\.)){4}$/));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class Ipv6 extends Character {
|
||
|
constructor() {
|
||
|
super(
|
||
|
new RegExp(
|
||
|
/^([0-9a-fA-F]{1,4}:){7,7}([0-9a-fA-F]{1,4}|:)|([0-9a-fA-F]{1,4}:){1,6}(:[0-9a-fA-F]{1,4}|:)|([0-9a-fA-F]{1,4}:){1,5}((:[0-9a-fA-F]{1,4}){1,2}|:)|([0-9a-fA-F]{1,4}:){1,4}((:[0-9a-fA-F]{1,4}){1,3}|:)|([0-9a-fA-F]{1,4}:){1,3}((:[0-9a-fA-F]{1,4}){1,4}|:)|([0-9a-fA-F]{1,4}:){1,2}((:[0-9a-fA-F]{1,4}){1,5}|:)|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6}|:)|:((:[0-9a-fA-F]{1,4}){1,7}|:)/
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export class IdCard {
|
||
|
trigger = 'blur';
|
||
|
validator = (rule: any, value: any, callback: any) => {
|
||
|
if (value != null && value.length != 0) {
|
||
|
let iSum = 0;
|
||
|
const aCity = {
|
||
|
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: '国外',
|
||
|
};
|
||
|
if (!/^\d{17}(\d|x)$/i.test(value)) {
|
||
|
callback(new Error('你输入的身份证长度或格式错误'));
|
||
|
}
|
||
|
value = value.replace(/x$/i, 'a');
|
||
|
if (aCity[parseInt(value.substr(0, 2))] == null) {
|
||
|
callback(new Error('你的身份证地区非法'));
|
||
|
}
|
||
|
|
||
|
const sBirthday =
|
||
|
value.substr(6, 4) +
|
||
|
'-' +
|
||
|
Number(value.substr(10, 2)) +
|
||
|
'-' +
|
||
|
Number(value.substr(12, 2));
|
||
|
const d = new Date(sBirthday.replace(/-/g, '/'));
|
||
|
if (
|
||
|
sBirthday !=
|
||
|
d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
|
||
|
) {
|
||
|
callback(new Error('身份证上的出生日期非法'));
|
||
|
}
|
||
|
for (let i = 17; i >= 0; i--) {
|
||
|
iSum += (Math.pow(2, i) % 11) * parseInt(value.charAt(17 - i), 11);
|
||
|
}
|
||
|
if (iSum % 11 != 1) {
|
||
|
callback(new Error('你输入的身份证号非法'));
|
||
|
}
|
||
|
}
|
||
|
callback();
|
||
|
};
|
||
|
}
|