|
| 1 | +## 1.x 升级到 2.x 指南 |
| 2 | + |
| 3 | +2.x 和 1.x 版本有所差别,如果您要升级到 2.x 版本,请根据以下内容做修改。 |
| 4 | + |
| 5 | +### 添加自定义规则 |
| 6 | +> WeValidator.addRule |
| 7 | +
|
| 8 | +1.x 版本 |
| 9 | +```javascript |
| 10 | +WeValidator.addRule('theRuleName', function(value, param){ |
| 11 | + return /\d/.test(value) |
| 12 | +}) |
| 13 | +``` |
| 14 | +2.x 版本 |
| 15 | +```javascript |
| 16 | +WeValidator.addRule('theRuleName', { |
| 17 | + message: '默认错误提示文字', |
| 18 | + rule(value, param){ |
| 19 | + return /\d/.test(value) |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +// 或者 |
| 24 | +WeValidator.addRule('theRuleName', { |
| 25 | + message: '默认错误提示文字', |
| 26 | + rule: /\d/ |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +### 单独校验某个内容 |
| 31 | +> WeValidator.checkValue |
| 32 | +
|
| 33 | +1.x 版本 |
| 34 | +```javascript |
| 35 | +let b1 = WeValidator.mobile('str') |
| 36 | +let b2 = WeValidator.chinese('str') |
| 37 | +``` |
| 38 | +2.x 版本 |
| 39 | +```javascript |
| 40 | +let b1 = WeValidator.checkValue('mobile', 'str') |
| 41 | +let b2 = WeValidator.checkValue('chinese', 'str') |
| 42 | +``` |
| 43 | + |
| 44 | +### 默认校验规则的变化 |
| 45 | +2.x 的校验规则大部分重写,删除了不常用的规则,并添加了新的规则 |
| 46 | + |
| 47 | +##### 删除的规则(可使用 2.x 相关规则替代) |
| 48 | +```diff |
| 49 | +- bankCard: true |
| 50 | ++ range: [16, 19] |
| 51 | + |
| 52 | +- mobileWithSpace |
| 53 | ++ pattern: /^1\d{2}\s?\d{4}\s?\d{4}$/ |
| 54 | + |
| 55 | +- noZeroStart |
| 56 | ++ pattern: /^([1-9][0-9]*)$/ |
| 57 | + |
| 58 | +- specialStr |
| 59 | ++ pattern: /[^%&',;=?$\x22]+/ |
| 60 | + |
| 61 | +- money |
| 62 | ++ pattern: /^\d+\.\d{2}$/ |
| 63 | + |
| 64 | +- month |
| 65 | ++ pattern: /^(0?[1-9]|1[0-2])$/ |
| 66 | + |
| 67 | +- day |
| 68 | ++ pattern: /^((0?[1-9])|((1|2)[0-9])|30|31)$/ |
| 69 | + |
| 70 | +- html |
| 71 | ++ pattern: /<(.*)>(.*)<\/(.*)>|<(.*)\/>/ |
| 72 | + |
| 73 | +- spaceEnter |
| 74 | ++ pattern: /\n[\s| ]*\r/ |
| 75 | + |
| 76 | +- qq |
| 77 | ++ pattern: /^[1-9][0-9]{4,}$/ |
| 78 | + |
| 79 | +- zip |
| 80 | ++ pattern: /^[\d]{6}/ |
| 81 | + |
| 82 | +- doubleByte |
| 83 | ++ pattern: /[^\x00-\xff]/ |
| 84 | + |
| 85 | +- intLength: 2 |
| 86 | ++ pattern: /^\d{2}$/ |
| 87 | + |
| 88 | +- decimalLength: 2 |
| 89 | ++ pattern: /^[0-9]+(.[0-9]{2}$/ |
| 90 | + |
| 91 | +- decimalLengthRange: [2, 4] |
| 92 | ++ pattern: /^[0-9]+(.[0-9]{2,4}$/ |
| 93 | + |
| 94 | +- stringLetter: 'a' |
| 95 | ++ pattern: /^[a-z]+$/ |
| 96 | + |
| 97 | +- stringLetterDefault |
| 98 | ++ pattern: /^\w+$/ |
| 99 | +``` |
| 100 | + |
| 101 | +##### 规则名称变化 |
| 102 | +```diff |
| 103 | +- idCard |
| 104 | ++ idcard |
| 105 | + |
| 106 | +- chinese2to8: true |
| 107 | ++ rangeChinese: [2, 8] |
| 108 | + |
| 109 | +- intOrFloat |
| 110 | ++ number |
| 111 | + |
| 112 | +- int |
| 113 | ++ digits |
| 114 | + |
| 115 | +- httpUrl |
| 116 | ++ url |
| 117 | + |
| 118 | +- equal: WeValidator.$value('field') |
| 119 | ++ equalTo: 'field' |
| 120 | + |
| 121 | +- intLessLength: 2 |
| 122 | ++ min: 2 |
| 123 | + |
| 124 | +- intGreater: 2 |
| 125 | ++ max: 2 |
| 126 | + |
| 127 | +- intLengthRange: [2, 6] |
| 128 | ++ range: [2, 6] |
| 129 | + |
| 130 | +- stringLength: 6 |
| 131 | ++ length: 6 |
| 132 | +``` |
| 133 | + |
| 134 | +##### 2.x 新增规则[参考](./README.md#默认支持的规则) |
0 commit comments