vaildation.ts 10.2 KB
class FormValidationForm{

    //身份证验证
    readonly idCardReg:RegExp = /^\d{17}[\dXx]$/;

    //银行卡号验证
    readonly bankNoReg:RegExp = /^\d{15,19}$/;

    //座机号码验证
    readonly telReg:RegExp = /^\d{3,4}-\d{7,8}$/;

    //联系号码验证
    readonly linktelReg:RegExp = /^1\d{10}$|^\d{3,4}-\d{7,8}$/;

    //手机号码验证
    readonly cellphoneReg:RegExp = /^1\d{10}$/;

    //微信号或手机号验证
    readonly wechatReg:RegExp = /^[1]\d{10}$|^[a-zA-Z][\da-zA-Z_-]{5,19}$/;

    //QQ号验证
    readonly qqReg:RegExp = /^\d{5,12}$/;

    //年龄验证
    readonly ageReg:RegExp = /^0$|^[1-9]\d{0,2}$/;

    //金额1验证
    readonly money1Reg:RegExp = /^0$|^[1-9]\d{0,8}$/;

    //金额2验证
    readonly money2Reg:RegExp = /^0$|^(([1-9]\d{0,8})|0)(\.\d{1,2})?$/;

    //邮编验证
    readonly postReg:RegExp = /^\d{6}$/;

    //电子邮箱验证
    readonly emailReg:RegExp = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;

    checkCellPhone(value: any) {
        return this.cellphoneReg.test(value)
    }
    Form(){
        let _this = this
        return {
            checkIdCard_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.idCardReg.test(value)) {
                    callback(new Error('请输入正确的身份证号码'))
                } else {
                    callback()
                }
            },
            checkIdCard_N (rule: any, value:any, callback:any) {
                if (value && !_this.idCardReg.test(value)) {
                    callback(new Error('请输入正确的身份证号码'))
                } else {
                    callback()
                }
            },
            checkBankNo_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.bankNoReg.test(value)) {
                    callback(new Error('请输入正确的银行卡号'))
                } else {
                    callback()
                }
            },
            checkBankNo_N (rule: any, value:any, callback:any) {
                if (value && !_this.bankNoReg.test(value)) {
                    callback(new Error('请输入正确的银行卡号'))
                } else {
                    callback()
                }
            },
            checkTel_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.telReg.test(value)) {
                    callback(new Error('请输入区号-号码'))
                } else {
                    callback()
                }
            },
            checkTel_N (rule: any, value:any, callback:any) {
                if (value && !_this.telReg.test(value)) {
                    callback(new Error('请输入区号-号码'))
                } else {
                    callback()
                }
            },
            checkLinkTel_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.linktelReg.test(value)) {
                    callback(new Error('请输入手机号或座机号(区号-号码)'))
                } else {
                    callback()
                }
            },
            checkLinkTel_N (rule: any, value:any, callback:any) {
                if (value && !_this.linktelReg.test(value)) {
                    callback(new Error('请输入手机号或座机号(区号-号码)'))
                } else {
                    callback()
                }
            },
            checkCellPhone_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.cellphoneReg.test(value)) {
                    callback(new Error('请输入正确的手机号码'))
                } else {
                    callback()
                }
            },
            checkCellPhone_N (rule: any, value:any, callback:any) {
                if (value && !_this.cellphoneReg.test(value)) {
                    callback(new Error('请输入正确的手机号码'))
                } else {
                    callback()
                }
            },
            checkWechat_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.wechatReg.test(value)) {
                    callback(new Error('请输入正确的微信号或手机号'))
                } else {
                    callback()
                }
            },
            checkWechat_N (rule: any, value:any, callback:any) {
                if (value && !_this.wechatReg.test(value)) {
                    callback(new Error('请输入正确的微信号或手机号'))
                } else {
                    callback()
                }
            },
            checkQQ_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.qqReg.test(value)) {
                    callback(new Error('请输入正确的QQ号'))
                } else {
                    callback()
                }
            },
            checkQQ_N (rule: any, value:any, callback:any) {
                if (value && !_this.qqReg.test(value)) {
                    callback(new Error('请输入正确的QQ号'))
                } else {
                    callback()
                }
            },
            checkAge_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.ageReg.test(value)) {
                    callback(new Error('请输入3位以内的自然数'))
                } else {
                    callback()
                }
            },
            checkAge_N (rule: any, value:any, callback:any) {
                if (value && !_this.ageReg.test(value)) {
                    callback(new Error('请输入3位以内的自然数'))
                } else {
                    callback()
                }
            },
            checkMoney1_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.money1Reg.test(value)) {
                    callback(new Error('请输入9位以内的自然数'))
                } else {
                    callback()
                }
            },
            checkMoney1_N (rule: any, value:any, callback:any) {
                if (value && !_this.money1Reg.test(value)) {
                    callback(new Error('请输入9位以内的自然数'))
                } else {
                    callback()
                }
            },
            //改错误提示请根据具体项目确定
            checkMoney2_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.money2Reg.test(value)) {
                    callback(new Error('格式错误,如200.00'))
                } else {
                    callback()
                }
            },
            checkMoney2_N (rule: any, value:any, callback:any) {
                if (value && !_this.money2Reg.test(value)) {
                    callback(new Error('格式错误,如200.00'))
                } else {
                    callback()
                }
            },
            checkLoan (rule: any, value:any, callback:any) {
                if (value && !_this.money2Reg.test(value)) {
                    callback(new Error('请输入正确的贷款金额'))
                } else {
                    callback()
                }
            },
            checkGet (rule: any, value:any, callback:any) {
                if (value && !_this.money2Reg.test(value)) {
                    callback(new Error('请输入正确的放款金额'))
                } else {
                    callback()
                }
            },
            checkPost_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.postReg.test(value)) {
                    callback(new Error('请输入正确的邮编'))
                } else {
                    callback()
                }
            },
            checkPost_N (rule: any, value:any, callback:any) {
                if (value && !_this.postReg.test(value)) {
                    callback(new Error('请输入正确的邮编'))
                } else {
                    callback()
                }
            },
            checkEmail_R (rule: any, value:any, callback:any) {
                if (!value) {
                    return callback(new Error('必填'))
                }
                if (!_this.emailReg.test(value)) {
                    callback(new Error('请输入正确的电子邮箱'))
                } else {
                    callback()
                }
            },
            checkEmail_N (rule: any, value:any, callback:any) {
                if (value && !_this.emailReg.test(value)) {
                    callback(new Error('请输入正确的电子邮箱'))
                } else {
                    callback()
                }
            },
            checkEmail_QQ_N (rule: any, value:any, callback:any) {
                if (value) {
                    if (_this.qqReg.test(value) || _this.emailReg.test(value)) {
                        callback()
                    } else {
                        callback(new Error('请输入正确的QQ或电子邮箱'))
                    }
                } else callback()
            },
        }
    }
}
export const formValidationForm = new FormValidationForm()
//测试
// new FormValidationForm().Form().checkAge_N()
//FormValidationForm.inst.Form().checkAge_N()