正则验证

// 用于组装组合规则
export const packageRuleFactory = (ruleList: any[]) => ({
  validator: (_: any, value: any, callback: any) => {
    for (let i = 0; i < ruleList.length; i++) {
      const rule = ruleList[i];
      if (rule.required && (!value || !value.length)) {
        callback(rule.message);
        return;
      }
      if (rule.message && !rule.validator(_, value)) {
        callback(rule.message);
        return;
      } else {
        rule.validator(_, value, callback);
        return;
      }
    }
    callback();
  },
});

export const TextRuleFactory = ({
  requiredMessage,
  type,
  size,
  max,
  min,
  minX,
  withOutZero,
}: {
  requiredMessage?: string;
  size?: 'default' | 'large' | number;
  withOutZero?: boolean;
  max?: number;
  min?: number;
  minX?:number;
  type?:
    | 'integer'
    | 'money'
    | 'phone'
    | 'tel'
    | 'letter'
    | 'onlyNumber'
    | 'scoreRule'
    | 'limitNumber'
    | 'priceNumber'
    | 'sumNumber'
    | 'sumTotNumber'
    | 'priceSumNumber';
}) => {
  const rules: any = [{ required: !!requiredMessage, message: requiredMessage }];
  if (size && typeof size == 'number') {
    rules.push(
      lengthRule({
        max: size,
        message: `长度不应超过${size}个字符`,
      }),
    );
  } else {
    const sizeNumber = {
      default: 100,
      large: 1000,
    }[size || 'default'];
    rules.push(
      lengthRule({
        max: sizeNumber,
        message: `长度不应超过${sizeNumber}个字符`,
      }),
    );
  }
  const validator: any = type
    ? [
        {
          money: priceNumber,
          tel: telRule,
          phone: phoneRule,
          integer: integer,
          letter: LetterRule,
          onlyNumber: onlyNumber,
          limitNumber: limitNumber,
          scoreRule: scoreRule,
          sumNumber: sumNumber,
          sumTotNumber: sumTotNumber,
          priceSumNumber: priceSumNumber,
        }[type],
      ]
    : [];
  if (typeof max == 'number') {
    validator.push({
      validator: (_: any, value: string, callback: Function) => {
        if (!isNaN(Number(value)) && max < Number(value)) {
          callback(`请输入小于等于${max}的数字`);
        } else callback();
      },
    });
  }
  if (typeof min == 'number') {
    validator.push({
      validator: (_: any, value: string, callback: Function) => {
        if (typeof value == 'number' && min > value) {
          callback(`请输入大于等于${max}的数字`);
        } else callback();
      },
    });
  }
  if (typeof minX == 'number') {
    validator.push({
      validator: (_: any, value: number, callback: Function) => {
        if (!isNaN(Number(value)) && minX > value) {
          callback(`请输入大于等于${minX}的数字`);
        } else callback();
      },
    });
  }
  if (withOutZero) {
    validator.push({
      validator: (_: any, value: string, callback: Function) => {
        if (value == '0') {
          callback(`不能为0`);
        } else callback();
      },
    });
  }
  return [...rules, ...validator];
};

export const uploadFileNameRuleFactory = (message?: string) => {
  return {
    validator: (_: any, value: any) =>
      !value.length || value.every((file: any) => file.fileName || !file.blur),
    message: message || '文件名称不能为空',
  };
};

export const lengthRule = ({ max, message }: { max: number; message: string }) => {
  return {
    validator: (rule: any, value: string, callback: (messgae?: string) => void) => {
      if (!value || String(value).length <= max) callback();
      else callback(message);
    },
  };
};
export const checkPassword = (rule: any, value: string, callback: (messgae?: string) => void) => {
  const str = /^(?=.*[a-zA-Z])(?=.*[1-9]).{6,16}$/;
  if (!value) {
    callback();
  } else {
    if (value.length < 6) {
      callback('密码长度应为6~16个字符');
    } else if (value.length > 16) {
      callback('密码长度应为6~16个字符');
    } else if (!str.test(value)) {
      callback('密码过于简单,请尝试“字母+数字”的组合');
    } else {
      callback();
    }
  }
};

//校验数字范围  保留小数点后六位
export const validateAcquaintance = {
  validator: (rule: any, value: string, callback: Function) =>
    !value || /^[0-9]+(.[0-9]{1,6})?$/.test(value),
  message: '小数后面只能保留六位',
};

export const phoneRule = {
  validator: (rule: any, value: string, callback: Function) => {
    const result = !value || /^1[3-9]{1}[0-9]{9}$/.test(value);
    callback(result ? undefined : '请输入如示手机号码:13888888888');
  },
};

export const telRule = {
  validator: (rule: any, value: string, callback: Function) => {
    const result =
      !value ||
      /^1[3-9]{1}[0-9]{9}$/.test(value) ||
      /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value);
    callback(result ? undefined : '请输入如示联系方式:010-88888888或13888888888');
  },
};

export const LetterRule = {
  validator: (rule: any, value: any, callback: any) =>
    !value ||
    !/[`~\s!@#$%^&*()_\+=<>?:"{}|,.\/;'\\[\]·~!@#¥%……&*()——\+={}|《》?:“”【】、;‘',。、]/.test(
      value,
    ),
  message: '输入项包含空格或特殊字符',
};

export const LetterUrlRule = {
  validator: (rule: any, value: any, callback: any) =>
    !value ||
    !/[`~!@#$%^&*()_\-+=<>?:"{}|,.;'\\[\]·~!@#¥%……&*()——\-+={}|《》?:“”【】、;‘',。、]/.test(
      value,
    ),
  message: '输入项包含特殊字符',
};
export const menuUrlRule = {
  validator: (rule: any, value: any, callback: any) =>
    !value ||
    !/[`~!@$%^&*()_\-+=<>?"{}|,.;'\\[\]·~!@¥%……&*()——\-+={}|《》?:“”【】、;‘',。、]/.test(
      value,
    ),
  message: '输入项包含特殊字符',
};

export const only = {
  validator: (rule: any, value: any, callback: any) =>
    !value || !/[^\a-zA-Z\u4E00-\u9FA5]/g.test(value),
  message: '只能输入中文或英文字母',
};
//菜单图标
export const onlyEnglish = {
  validator: (rule: any, value: any, callback: any) => !value || /^[A-Za-z]+$/g.test(value),
  message: '只能输入英文字母',
};
//菜单地址
export const menuUrl = {
  validator: (rule: any, value: any, callback: any) => !value || /^[A-Za-z\/# ]+$/g.test(value),
  message: '输入项包含特殊字符',
};

export const integer = {
  validator: (_: any, value: any, callback: any) => {
    const result = !value || value == '0' || /^([1-9][0-9]*)$/g.test(value);
    callback(result ? undefined : '只能输入0或正整数');
  },
};

// 数字保留后两位  咨询费用
export const KeepDecimal = {
  validator: (rule: any, value: string, callback: Function) => {
    if (value && !/^[+]?\d*[.]?\d{0,2}$/.test(value)) {
      callback('只能输入正数数字并保留小数点后两位');
    } else if (value && Number(value) > 100000) {
      callback('数字不能大于100000');
    } else {
      callback();
    }
  },
};

export const MaxNum = {
  validator: (rule: any, value: string, callback: Function) => !value || Number(value) <= 99,
  message: '最大值只能输入99',
};
//只能输入数字
export const onlyNumber = {
  validator: (rule: any, value: string, callback: Function) => {
    if (!value || (/^[0-9]*$/g.test(value) && Number(value) > 0)) {
      callback();
    } else callback('只能输入正整数');
  },
};

//只能输入6位带两个小数点
export const limitNumber = {
  validator: (rule: any, value: any, callback: any) =>
    !value || (/^(-?[0-9]{1,6}(\.[0-9]{1,2})?)$/g.test(value) && value != '-0'),
  message: '请输入如示的格式:999999.99',
};
//只能正数带小数点--价钱//不能以多个0开头
export const priceNumber = {
  validator: (rule: any, value: any, callback: any) => {
    const result =
      !value ||
      /^(0(\.[0-9]{0,1})[1-9])$/.test(value) ||
      /^(?!0{2,})(?:\d{1,6}(\.[0-9]{1,2})?|10000)$/.test(value);
    callback(result ? undefined : '请输入如示的格式:999999.99');
  },
};
//只能正数带小数点--价钱不限制位数
export const priceSumNumber = {
  validator: (rule: any, value: any, callback: any) =>
    !value || (/^((0|[1-9]\d*)(\.\d+)?)$/g.test(value) && value != '0'),
  message: '请输入有效的数字',
};
//只能正数--不能有小数点 --数量
export const sumNumber = {
  validator: (rule: any, value: any, callback: any) =>
    !value || (/^([1-9])\d{0,5}$/g.test(value) && value != '0'),
  message: '请输入如示的格式:999999',
};
export const sumTotNumber = {
  validator: (rule: any, value: any, callback: any) =>
    !value || (/^([0-9]{1,9})$/g.test(value) && value != '0'),
  message: '请输入如示的格式:999999999',
};
export const onlyPositiveNumber = {
  validator: (rule: any, value: any, callback: any) =>
    !value || /^(0|[1-9]\d*)(\.\d+)?/g.test(value),
  message: '请输入有效的数字',
};

export const letterLength = {
  validator: (rule: any, value: any, callback: any) => !value || value.trim().length <= 10,
  message: '长度不应超过10个字符',
};

export const latlng = {
  validator: (rule: any, value: any, callback: any) =>
    !value ||
    /^[EW]?((\d|[1-9]\d|1[0-7]\d)[\s\-,;°度](\d|[0-5]\d)[\s\-,;′分](\d|[0-5]\d)(\.\d{1,2})?[\s\-,;\"秒]?$)|(180[\s\-,;°度]0[\s\-,;′分]0[\s\-,;\"秒]?$)/g.test(
      value,
    ),
  message: '请输入正确的经纬度',
};

export const scoreRule = {
  validator: (rule: any, value: any, callback: any) =>
    !value || /^(?:(?!0\d)\d{1,2}(?:\.\d{1,2})|100(?:\.0{1,2}))$/g.test(value),
  message: '请输入如示考核得分:100.00',
};
posted @ 2021-01-04 22:02  外行的小白  阅读(167)  评论(0)    收藏  举报