|
1 |
| -## based on validate before form submit |
| 1 | +## 基于表单提交前验证的库 |
2 | 2 |
|
| 3 | +灵感来自于mongoose.Schema 实现一个配置型的表单验证库 |
3 | 4 | ####
|
4 |
| -<b>Inspired</b> by mongoose.Schema, achieve an <b>config type</b> form validation library |
5 |
| -#### |
6 |
| -[中文说明](https://github.com/pagewang0/form-validation/blob/master/README_CN.md) |
| 5 | +[English](https://github.com/pagewang0/form-validation/blob/master/README_EN.md) |
7 | 6 |
|
8 |
| -### |
9 | 7 | Feature:
|
10 |
| -* single input item validate (synchronous and asynchronous) |
11 |
| -* based on validate before form submit |
12 |
| -* provide field.get method get field value |
13 |
| -* DIY validation method |
14 |
| - |
15 |
| -```javascript |
16 |
| -var validator = new Validator({ |
17 |
| - username: {required: true, length: {max: 11, min: 6}}, |
18 |
| - password: {required: true, length: {max: 11, min: 6}}, |
19 |
| - confirm: {equal: 'password'}, |
20 |
| - email: {required: true, type: 'email'}, |
21 |
| - sex: {required: true, enum: ['男', '女', '保密']}, |
22 |
| - phone: {required: true, type: Number, length: 11}, |
23 |
| -}) |
24 |
| - |
25 |
| -var messages = validator.messages({ |
26 |
| - username: {required: '用户名是必须的', length: {max: '用户名长度超过最大值', min: '用户名长度小于最小值'}}, |
27 |
| - password: {required: '密码是必须的', length: {max: '密码长度超过最大值', min: '密码长度小于最小值'}}, |
28 |
| - confirm: {equal: '确认密码必须和密码保持一致'}, |
29 |
| - email: {required: 'email是必须的', type: 'email格式不正确'}, |
30 |
| - sex: {required: '性别是必须的', enum: '性别输入值不在限定范围之内'}, |
31 |
| - phone: {required: '手机号码是必须的', type: '手机号码值类型错误', length: '手机号码长度必须为11'} |
32 |
| -}) |
33 |
| - |
34 |
| -validator.path('username').validate(function(field, username, done) { |
35 |
| - request('/username') |
36 |
| - .query({username: username}) |
37 |
| - .end((err, res) => { |
38 |
| - if (err) { |
39 |
| - done(err, field) |
40 |
| - return |
41 |
| - }; |
42 |
| - done() |
43 |
| - }) |
44 |
| -}, '用户名已存在') |
45 |
| -``` |
| 8 | +* 提供基本验证功能 |
| 9 | +* 提供可扩展的验证方法来支持同步/异步验证 |
| 10 | +* 错误信息/样式的set/reset |
| 11 | +* 通用/指定的获取字段值方法 |
| 12 | +* 支持单个字段的同步/异步验证 |
| 13 | +* 提供submit callback方法 |
| 14 | +* 提供获取error信息对象集合 |
46 | 15 |
|
47 |
| -ps: I am english is poor, please forgive... |
| 16 | +基本用法见example, 提供了 |
| 17 | +[base](https://github.com/pagewang0/form-validation/tree/master/examples/base), [angularjs](https://github.com/pagewang0/form-validation/tree/master/examples/angularjs), [react](https://github.com/pagewang0/form-validation/tree/master/examples/react) |
| 18 | +三种用法 |
0 commit comments