General regular expressions: required fields must be filled in according to the rules, non-mandatory fields must be empty or filled in according to the rules

When developing the title, sometimes it is necessary to fill in mandatory and optional fields. When it is optional, either leave it blank or fill it in according to the rules. At this time, you can add a judgment condition and return when it is empty.

{
    
    
  type: 'string',
  message: '长度2-15个汉字或者字母',
  trigger: 'blur',
  transform (value) {
    
    
   // 不填为空就return
   if (value == '') {
    
    
      return
   }
   if (!/^[a-zA-Z\u4e00-\u9fa5]{
    
    2,15}$/.test(value)) {
    
    
      return true
    } else {
    
    
    }
  }
},

Record the regular expressions commonly used in the title:

  1. 4-15 letters: /[a-zA-Z]{4,15}$/
  2. 6-16 numbers, letters, ".", "@", "!", "_": [a-zA-Z0-9.@!_/]{6,16}$
  3. IPv4 address rules: ^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(.((2(5[0- 5]|[0-4]\d))|[0-1]?\d{1,2})){3}$
  4. Port number rule, range 0-65535: ^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3} |65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
  5. Comply with the web link rules: [a-zA-z]+://[^\s]*
  6. Length 2-15 Chinese characters or letters: [a-zA-Z\u4e00-\u9fa5]{2,15}$
  7. Length 5-15 letters, numbers, "_": [a-zA-Z0-9_/]{5,15}$
  8. Length 10-15 letters, numbers: [a-zA-Z0-9]{10,15}$

Online regular expression detection tool URL

Guess you like

Origin blog.csdn.net/weixin_55966654/article/details/125321412