使用方式:
源码:
//电话号码
export const checkModbile = (rule,value,callback)=>{
if(value == undefined)
{
callback();
return;
}
if(value.length === 11){
var reg = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
}else if(value.length === 13 || value.length === 8 || value.length === 7){
var reg = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/;
}else{
var reg = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
}
if(value===''){
callback(new Error('电话号码不能为空'))
}else if (!reg.test(value)) {
callback(new Error('请输入正确的电话号码'))
}else{
callback()
}
}
//邮箱
export const checkEmil = (rule, value, callback) => {
let regTest = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (!value) {
callback()
} else {
if (!regTest.test(value)) {
callback(new Error("电子邮箱格式不正确"));
return;
}
callback();
}
};
//统一社会码
export const checkSocialcode = (rule, value, callback) => {
var regTest = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/;
if (!value) {
callback()
} else {
if (!regTest.test(value)) {
callback(new Error("统一社会信用代码格式不正确"));
return;
}
callback();
}
};
//身份证
export const checkID = (rule, value, callback) => {
var regTest = /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/ ;
if (!value) {
callback()
} else {
if (!regTest.test(value)) {
callback(new Error("身份证格式不正确"));
return;
}
callback();
}
};
//名称长度
export const checkNamelength = (rule, value, callback) => {
var regTest = 255
if(value.length == 0){
callback(new Error("公司名称不能为空"));
} else if(value.length <= regTest) {
callback()
}else {
callback(new Error("公司名称长度不能超过255个字符"));
return;
}
callback();
};
//联系人长度
export const checkLxrlength = (rule, value, callback) => {
var regTest = 255
if(value.length == 0){
callback(new Error("联系人不能为空"));
} else if(value.length <= regTest) {
callback()
}else {
callback(new Error("联系人长度不能超过255个字符"));
return;
}
callback();
};
export const checklength = (rule, value, callback) => {
var regTest = 255
if(value.length == 0){
callback(new Error("不能为空"));
} else if(value.length <= 255) {
callback()
}else {
callback(new Error("长度不能超过255个字符"));
return;
}
callback();
};
//建设规模
export const checkJSGM = (rule, value, callback) => {
let regExp = /^\d*\.{0,8}\d{0,8}$/;
if (value === "" || value === null || regExp.test(value) === false) {
callback(new Error("请填写建设规模且只能填写数字"));
} else {
callback();
}
};
// 数字小数点前后位数
export const testNumber = (rule,value,callback,)=>{
/* rule中:
integer: 8,//整数位
decimal: 4,//小数位*/
if(value == undefined)
{
callback();
return;
}
let newarr = value.toString().split("");
if(newarr.length >1 && newarr[0] == 0 && !newarr.includes(".")) {
callback(new Error("首位不能为0"));
} else if(newarr.includes(".")){
let int = value.toString().split(".")[0];
let dec = value.toString().split(".")[1];
if (int.length > rule.integer) {
callback(new Error("整数位不能大于" + rule.integer + "位"));
}
if (dec.length > rule.decimal) {
callback(new Error("小数位不能大于" + rule.decimal + "位"));
}
callback();
}else {
if (newarr.length > rule.integer) {
callback(new Error("整数位不能大于" + rule.integer + "位"));
} else {
callback();
}
}
}
// 银行卡账号校验
export const testBankCard = (rule,value,callback,)=> {
let newNumber = value.toString().split("");
if(newNumber.length === 0){
callback(new Error("银行账号不能为空"));
}else if(newNumber.length<16){
callback(new Error("请填写正确的银行卡账号"));
}
let cardNum = /^[1-9]\d{9,29}$/ ;
if (value === "" || value === null || cardNum.test(value) === false) {
callback(new Error("请填写正确的银行卡账号"));
} else {
callback();
}
}
//校验字符
export const checkCommon = (rule,value,callback,)=>{
/**
rule中:
tipWord: 提示字段名 String
wordLength: (最大)长度信息 Number
wordMinLength: 最小长度信息,可不填,即无最小字符长度限制
**/
let newArr = value.toString().split("");
if(rule.wordMinLength){
if(newArr.length < rule.wordMinLength) {
callback( rule.tipWord + "长度不能低于" + rule.wordMinLength + "个字符")
}
}
if(rule.required === true && newArr.length === 0){
callback(new Error(rule.tipWord + "不能为空"));
} else if(newArr.length >= rule.wordLength) {
callback( rule.tipWord + "长度不能超过" + rule.wordLength + "个字符")
}
callback();
}