The premise

In vUE development, it is inevitable to encounter a variety of form verification, here sorted out the network and their usual high frequency of use of some verification methods. If the error is welcome to point out, the late continuous supplement update.

Git address

Front – end regular check

Git address

Welcome to the front of the public account [Xiao Yao students]

1. Check whether the IP address is valid

export function validateIP(rule, value,callback) { if(value==''||value==undefined||value==null){ callback(); } else {const reg = / ^ (\ d {1, 2} \ d \ d | 2 | 1 \ | d 25 [0-4] [0-5]) \. (\ d {1, 2} \ d \ d | 2 | 1 [0-4] \ | d 25 [0-5]) \! [](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/22/16e9182103044230~tplv-t2oaga2asx-image.imag E). (\ d {1, 2} \ d \ d | 2 | 1 \ | d 25 [0-4] [0-5]) \. (\ d {1, 2} \ d \ d | 2 | 1 [0-4] \ | d 25 [0-5]) $/; if ((! reg.test(value)) && value ! Callback (new Error(' please input correct IP address ')); } else { callback(); }}}Copy the code

2, whether the mobile phone number or fixed number

The export function validatePhoneTwo (rule, the value, the callback) {const reg = / ^ ((0 \ d {2, 3} - \ d {7, 8}) | (1 [34578] \ d {9})) $/;; if (value == '' || value == undefined || value == null) { callback(); } else { if ((! reg.test(value)) && value ! Callback (new Error(' Please input the correct phone number or landline number ')); } else { callback(); }}}Copy the code

3. Whether it is a fixed line

Export function validateTelphone(rule, value,callback) {const reg =/0\d{2,3}-\d{7,8}/; if(value==''||value==undefined||value==null){ callback(); }else { if ((! reg.test(value)) && value ! Callback (new Error(' please input the correct landline number ')); } else { callback(); }}}Copy the code

4, whether the mobile phone number

export function validatePhone(rule, value,callback) { const reg =/^[1][3-9][0-9]{9}$/; if(value==''||value==undefined||value==null){ callback(); }else { if ((! reg.test(value)) && value ! Callback (new Error(' Please input the correct phone number ')); } else { callback(); }}}Copy the code

5, whether id card number

export function validateIdNo(rule, value,callback) { const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; if(value==''||value==undefined||value==null){ callback(); }else { if ((! reg.test(value)) && value ! Callback (new Error(' please input the correct id ')); } else { callback(); }}}Copy the code

6. Email address

export function validateEMail(rule, value,callback) { const reg =/^([a-zA-Z0-9]+[-_\.]?) +@[a-zA-Z0-9]+\.[a-z]+$/; if(value==''||value==undefined||value==null){ callback(); }else{ if (! Reg.test (value)){callback(new Error(' please input the correct email ')); } else { callback(); }}}Copy the code

7. Valid URLS

export function validateURL(url) { const urlregex = /^(https? |ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?) (\. (25 [0 to 5] | 2 [0 to 4] [0-9] [0-9] {2} | 1 | [1-9]? [0-9])){3}|([a-zA-Z0-9-]+\.) *[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA- Z0-9.,?'\\+&%$#=~_-]+))*$/; return urlregex.test(url); }Copy the code

8. Check whether the content contains digits and underscores (_)

export function isPassword(rule, value, callback) { const reg =/^[_a-zA-Z0-9]+$/; if(value==''||value==undefined||value==null){ callback(); } else { if (! Reg.test (value)){callback(new Error(' only letters, digits, and underscores ')); } else { callback(); }}}Copy the code

9. Automatically check the range of values

export function checkMax20000(rule, value, callback) { if (value == '' || value == undefined || value == null) { callback(); } else if (! Number(value)) {callback(new Error(' please input a Number between [1,20000] '); } else if (value < 1 | | value > 20000) {callback (new Error (" please enter a number between [1200] ')); } else { callback(); }}Copy the code

10. Verify the maximum value of the digital input box

export function checkMaxVal(rule, Value, the callback) {if (value < 0 | | value > maximum) {callback (new Error (" please enter a number between [0, the maximum] ')); } else { callback(); }}Copy the code

11. Verify whether it is between 1 and 99

export function isOneToNinetyNine(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' please input positive integer ')); } else {const re = /^[1-9][0-9]{0,1}$/; const rsCheck = re.test(value); if (! RsCheck) {callback(new Error(' please input a positive integer, value is [1,99] ')); } else { callback(); }}}, 0); }Copy the code

12. Verify whether it is an integer

export function isInteger(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' please input positive integer ')); } else { const re = /^[0-9]*[1-9][0-9]*$/; const rsCheck = re.test(value); if (! RsCheck) {callback(new Error(' Please enter a positive integer ')); } else { callback(); }}}, 0); }Copy the code

13. Verify whether it is an integer

export function isIntegerNotMust(rule, value, callback) { if (! value) { callback(); } setTimeout(() => { if (! Number(value)) {callback(new Error(' please input positive integer ')); } else { const re = /^[0-9]*[1-9][0-9]*$/; const rsCheck = re.test(value); if (! RsCheck) {callback(new Error(' Please enter a positive integer ')); } else { callback(); }}}, 1000); }Copy the code

14, verify whether the decimal is [0-1]

export function isDecimal(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' please input a Number between [0,1] ')); Value < 0} else {the if (| | value > 1) {callback (new Error (" please enter a number between [0, 1] ')); } else { callback(); }}}, 100); }Copy the code

15, verify whether it is [1-10] decimal, that is, can not equal to 0

export function isBtnOneToTen(rule, value, Callback) {if (typeof value == 'undefined') {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' please input positive integer, value is [1,10]')); } else { if (! (value == '1' || value == '2' || value == '3' || value == '4' || value == '5' || value == '6' || value == '7' || value Value = = = = '8' | | '9' | | value = = '10')) {callback (new Error (" please enter a positive integer, value of [1, 10] ')); } else { callback(); }}}, 100); }Copy the code

16, verify whether it is [1-100] decimal, that can not be equal to 0

export function isBtnOneToHundred(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' Please input integer, value is [1,100]')); } else {the if (value < 1 | | value > 100) {callback (new Error (" please enter an integer value for the [1100], ')); } else { callback(); }}}, 100); }Copy the code

17. Verify if it is a decimal from [0-100]

export function isBtnZeroToHundred(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); } setTimeout(() => { if (! Number(value)) {callback(new Error(' Please enter a Number between [1,100] ')); Value < 0} else {the if (| | value > 100) {callback (new Error (" please enter a number between [1100])); } else { callback(); }}}, 100); }Copy the code

18. Verify whether the port is between [0,65535]

export function isPort(rule, value, callback) { if (! Value) {return callback(new Error(' input cannot be null ')); = > {if} setTimeout () (value = = "| | typeof (value) = = undefined) {callback (new Error (" please enter the port value")); } else { const re = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/; const rsCheck = re.test(value); if (! RsCheck) {callback(new Error(' Please enter a port value between [0-65535] ')); } else { callback(); }}}, 100); }Copy the code

19. Check whether the port number is between 0 and 65535. This parameter is optional

export function isCheckPort(rule, value, callback) { if (! value) { callback(); = > {if} setTimeout () (value = = "| | typeof (value) = = undefined) {/ / callback (new Error (" please enter the port value")); } else { const re = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/; const rsCheck = re.test(value); if (! RsCheck) {callback(new Error(' Please enter a port value between [0-65535] ')); } else { callback(); }}}, 100); }Copy the code

20. Lowercase letters

export function validateLowerCase(val) {
  const reg = /^[a-z]+$/;
  return reg.test(val);
}
Copy the code

22. Two-digit decimal verification

const validateValidity = (rule, value, callback) => { if (! / (a ^ 1-9 [] ([0-9] +)? (\ [0-9] {1, 2})? $) | (^ (0) {1} $) | (^ \. [0-9] [0-9] [0-9]? $)/.test(value)) {callback(new Error(' up to two decimal places!! ')); } else { callback(); }};Copy the code

23. Whether to use capital letters

export function validateUpperCase(val) {
  const reg = /^[A-Z]+$/;
  return reg.test(val);
}
Copy the code

24, whether upper and lower case letters

export function validatAlphabets(val) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(val);
}
Copy the code

25. Password verification

export const validatePsdReg = (rule, value, callback) => { if (! Value) {return callback(new Error(' please input password '))} if (! / ^ (? ! [\d]+$)(? ! [a-zA-Z]+$)(? ! [^ \ da zA - Z] + $) ([^ \ u4e00 - \ u9fa5 \ s]) 6, 20} {$/. The test (value)) {callback (new Error (' please input 6 to 20 English letters, Numbers, or symbols (except Spaces), And at least two types of letters, numbers, and punctuation '))} else {callback()}}Copy the code

26. Chinese verification

export const validateContacts = (rule, value, callback) => { if (! Value) {return callback(new Error(' please input Chinese '))} if (! /^[\u0391-\ uffe5a-za-z]+$/.test(value)) {callback(new Error(' no special character '))} else {callback()}}Copy the code

27. Account verification

export const validateCode = (rule, value, callback) => { if (! Value) {return callback(new Error(' please input account '))} if (! / ^ (? ! ([0-9] * $)? ! [a zA - Z] * $) [a zA - Z0-9] 6, 20} {$/. The test (value)) {callback (new Error (' account must be a combination of letters and Numbers' 6-20))} else {callback ()}}Copy the code

28. Pure digital check

export const validateNumber = (rule, value, callback) => { let numberReg = /^\d+$|^\d+[.]? \d+$/ if (value ! == '') { if (! Numberreg.test (value)) {callback(new Error(' please input a number '))} else {callback(new Error(' please input a number ')}} else {callback(new Error(' please input a number '))}}Copy the code

29. One decimal place at most

const onePoint = (rule, value, callback) => { if (! / ^ [0-9] + [[.] {1} [0-9] {1})? $/.test(value)) {callback(new Error(' at most one decimal!! ')); } else { callback(); }};Copy the code

Usage:

Import import directly where it is used and then add it to the rules validation.

Front ac group

Add direct robot, after passing, reply with string to pull you into the group