{"id":33001917,"url":"https://github.com/ChanceYu/we-validator","last_synced_at":"2025-11-16T07:00:49.979Z","repository":{"id":45277345,"uuid":"143019817","full_name":"ChanceYu/we-validator","owner":"ChanceYu","description":":100: 简单灵活的表单验证插件，支持小程序、浏览器以及 Nodejs 端使用","archived":false,"fork":false,"pushed_at":"2023-05-17T14:27:13.000Z","size":214,"stargazers_count":240,"open_issues_count":0,"forks_count":53,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-10-17T17:14:28.449Z","etag":null,"topics":["miniprogram","node-validator","validation","validator","weapp","wechat","wechat-mini-program"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ChanceYu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-07-31T13:43:13.000Z","updated_at":"2025-06-30T05:36:31.000Z","dependencies_parsed_at":"2024-01-06T23:55:47.201Z","dependency_job_id":"72820a46-a0c9-4e88-9dcb-98751daeaf7c","html_url":"https://github.com/ChanceYu/we-validator","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/ChanceYu/we-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanceYu%2Fwe-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanceYu%2Fwe-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanceYu%2Fwe-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanceYu%2Fwe-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChanceYu","download_url":"https://codeload.github.com/ChanceYu/we-validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChanceYu%2Fwe-validator/sbom","scorecard":{"id":28439,"data":{"date":"2025-08-11","repo":{"name":"github.com/ChanceYu/we-validator","commit":"259499a615dbd0dc3129150a5f60979fc7fc9f50"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Code-Review","score":0,"reason":"Found 2/28 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 4 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'","Warn: branch protection not enabled for branch 'v2.1.10'","Warn: branch protection not enabled for branch 'v2.1.9'","Warn: branch protection not enabled for branch 'v2.1.8'","Warn: branch protection not enabled for branch 'v2.1.7'","Warn: branch protection not enabled for branch 'v2.1.6'","Warn: branch protection not enabled for branch 'v2.1.5'","Warn: branch protection not enabled for branch 'v2.1.4'","Warn: branch protection not enabled for branch 'v2.1.2'","Warn: branch protection not enabled for branch 'v2.1.1'","Warn: branch protection not enabled for branch 'v2.1.0'","Warn: branch protection not enabled for branch 'v1'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-14T18:28:09.340Z","repository_id":45277345,"created_at":"2025-08-14T18:28:09.341Z","updated_at":"2025-08-14T18:28:09.341Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284672648,"owners_count":27044736,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-16T02:00:05.974Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["miniprogram","node-validator","validation","validator","weapp","wechat","wechat-mini-program"],"created_at":"2025-11-13T14:00:24.545Z","updated_at":"2025-11-16T07:00:49.973Z","avatar_url":"https://github.com/ChanceYu.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"400\" src=\"assets/v2.png\" alt=\"we-validator\" /\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.org/ChanceYu/we-validator\"\u003e\u003cimg src=\"https://img.shields.io/travis/ChanceYu/we-validator/master.svg?color=green\u0026style=popout-square\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://opensource.org/licenses/mit-license.php\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/we-validator.svg?color=blue\u0026style=popout-square\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/we-validator\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/we-validator.svg?color=green\u0026style=popout-square\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003e 2.x 和 1.x 版本有所差别，如果您继续使用老版本，可以查看 [1.x 版本](https://github.com/ChanceYu/we-validator/tree/v1)。如果您要升级到 2.x 请先阅读[升级指南](./UPGRADE.md)，推荐使用 2.x 版本。\n\n简单灵活的表单验证插件，支持小程序、浏览器、Nodejs。小程序端支持：微信、支付宝、百度智能、字节跳动，小程序默认提示使用 `showToast`。\n\n[API 文档](#api) | [示例 Examples](#examples)，如果你 :sparkling_heart: 就点击 **★Star** 吧。\n\n## 特点\n\n- 使用简单灵活，不依赖任何框架\n- 既支持原生小程序方式，也支持 mpvue、wepy、taro 等小程序框架使用\n- 支持浏览器以及 Nodejs 端使用\n- [支持自定义规则](#wevalidatoraddrulerulename-ruleoption)\n- [支持自定义错误信息提示](#wevalidatoronmessage)\n- [支持动态添加或移除字段校验](#addrulesoptions)\n- [支持单独校验一个或多个字段规则](#checkfieldsdata-fields-onmessage)\n- [支持函数校验](#wevalidatorcheckvaluerulename-value-param)\n- [支持多个字段同时校验并显示错误](#多个字段同时校验并显示错误)\n- [默认支持常用校验规则](#默认支持的规则)\n\n\n\n## 安装\n使用 npm:\n\n```bash\nnpm install we-validator --save\n```\n\n使用 cdn:\n\n```html\n\u003cscript src=\"https://unpkg.com/we-validator/dist/we-validator.min.js\"\u003e\u003c/script\u003e\n```\n\n\n## 使用\n下面是微信小程序的用法，其它小程序类似\n\n\u003cdetails open\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```html\n\u003cform bindsubmit=\"onSubmitForm\"\u003e\n    \u003cinput type=\"text\" name=\"username\" placeholder=\"用户名\" /\u003e\n    \u003cinput type=\"number\" name=\"phoneno\" placeholder=\"手机号\" /\u003e\n    \u003cinput type=\"text\" name=\"str\" placeholder=\"长度为3的字符串\" /\u003e\n\n    \u003cbutton type=\"default\" formType=\"submit\"\u003e提交\u003c/button\u003e\n\u003c/form\u003e\n```\n\n```javascript\nconst WeValidator = require('we-validator')\n\nPage({\n    onReady(){\n        this.initValidator()\n    },\n    onSubmitForm(e){\n        let { value } = e.detail\n\n        if(!this.validatorInstance.checkData(value)) return\n\n        // 开始提交表单\n        // wx.request\n    },\n    initValidator(){\n        // 实例化\n        this.validatorInstance = new WeValidator({\n            rules: {\n                username: {\n                    required: true\n                },\n                phoneno: {\n                    required: true,\n                    mobile: true\n                },\n                str: {\n                    length: 3\n                },\n            },\n            messages: {\n                username: {\n                    required: '请输入用户名'\n                },\n                phoneno: {\n                    required: '请输入手机号',\n                    mobile: '手机号格式不正确'\n                },\n                str: { // 非必填字段\n                    length: '请输入长度为3的字符串'\n                },\n            },\n        })\n    },\n})\n```\n\n\u003c/details\u003e\n\n## Examples\n您也可参考当前项目下对应示例\n\n- [原生微信小程序使用方式](./example/wechat/pages/index/index.js)\n- [mpvue 使用方式](./example/mpvue/src/pages/index/index.vue)\n- [web 浏览器使用方式](./example/web/index.html)\n- [Nodejs 端使用方式](./example/nodejs/server/router/home.js)\n- [复杂的校验案例-自定义规则-动态添加字段校验](./example/complex/index.html)\n\n\n## API\n - [new WeValidator(options)](#new-wevalidatoroptions)\n    - [.checkData(data, onMessage)](#checkdatadata-onmessage) 校验数据，会显示错误信息\n    - [.checkFields(data, fields, onMessage)](#checkfieldsdata-fields-onmessage) 校验数据，会显示错误信息，只校验对应的字段\n    - [.isValid(data, fields)](#isvaliddata-fields) 校验数据，不会显示错误信息，可单独校验特定字段\n    - [.addRules(options)](#addrulesoptions) 动态添加字段校验\n    - [.removeRules(fields)](#removerulesfields) 动态移除字段校验\n\n\n## Static API\n - [WeValidator](#static-api)\n    - [.addRule(ruleName, ruleOption)](#wevalidatoraddrulerulename-ruleoption) 添加自定义规则\n    - [.checkValue(ruleName, value, param)](#wevalidatorcheckvaluerulename-value-param) 函数校验，单独校验某个内容\n    - [.onMessage](#wevalidatoronmessage) 设置全局错误信息显示\n\n\n## 默认支持的规则\n\n\u003e 注意: 非必填字段只有字段有值才会校验配置的规则\n\n| 规则 | 描述 | 默认提示 |\n| --- | --- | --- |\n| `required: true` | 必填 | 此字段必填 |\n| `pattern: /^\\d+$/` | 正则通用 | 不符合此验证规则 |\n| `email: true` | 电子邮件格式 | 请输入有效的电子邮件地址 |\n| `mobile: true` | 11位手机号 | 请输入11位的手机号码 |\n| `tel: true` | 座机号\u003cbr\u003e例如：010-1234567、0551-1234567 | 请输入座机号 |\n| `url: true` | URL网址 | 请输入有效的网址 |\n| `idcard: true` | 身份证号 | 请输入18位的有效身份证 |\n| `equalTo: 'field'` | 字段值相同校验\u003cbr\u003e例如：密码和确认密码，[参考](./example/web/index.html#L82) | 输入值必须和字段 `field` 相同 |\n| `notEqualTo: 'field'` | 字段值不能相同校验\u003cbr\u003e与 `equalTo` 相反 | 输入值不能和字段 `field` 相同 |\n| `contains: 'str'` | 是否包含某字符 | 输入值必须包含 `str` |\n| `notContains: 'str'` | 不能包含某字符 | 输入值不能包含 `str` |\n| `length: 5` | 长度为多少的字符串 | 请输入 `5` 个字符 |\n| `minlength: 2` | 最少多长的字符串 | 最少要输入 `2` 个字符 |\n| `maxlength: 6` | 最多多长的字符串 | 最多可以输入 `6` 个字符 |\n| `rangelength: [2, 6]` | 某个范围长度的字符串 | 请输入长度在 `2` 到 `6` 之间的字符 |\n| `number: true` | 数字 | 请输入有效的数字 |\n| `digits: true` | 正整数数字 | 只能输入正整数数字 |\n| `integer: true` | 正整数或负整数数字 | 只能输入整数数字 |\n| `min: 3` | 大于多少的数字\u003cbr\u003e（最小只能多少），也可以比较字段的值，[参考](./example/complex/index.html#L180) | 请输入大于 `3` 的数字 |\n| `max: 9` | 小于多少的数字\u003cbr\u003e（最大只能多少），也可以比较字段的值 | 请输入小于 `9` 的数字 |\n| `range: [3, 9]` | 大于且小于多少的数字 | 请输入大于 `3` 且小于 `9` 的数字 |\n| `chinese: true` | 中文字符 | 只能输入中文字符 |\n| `minChinese: 3` | 最少多少个中文字符 | 最少输入 `3` 个中文字符 |\n| `maxChinese: 9` | 最多多少个中文字符 | 最多输入 `9` 个中文字符 |\n| `rangeChinese: [3, 9]` | 大于且小于多少个中文字符 | 只能输入 `3` 到 `9` 个中文字符 |\n| `date: true` | 日期（默认使用 `new Date(value)` 校验） | 请输入有效的日期 |\n| `dateISO: true` | 日期（ISO标准格式）\u003cbr\u003e例如：2019-09-19，2019/09/19 | 请输入有效的日期（ISO标准格式） |\n| `ipv4: true` | ipv4地址 | 请输入有效的IPv4地址 |\n| `ipv6: true` | ipv6地址 | 请输入有效的IPv6地址 |\n\n\n## new WeValidator(options)\n\u003e 实例化\n\n**返回**: \u003ccode\u003eobject\u003c/code\u003e -  \u003ccode\u003evalidatorInstance\u003c/code\u003e\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| options | \u003ccode\u003eobject\u003c/code\u003e |  |  |\n| [options.rules] | \u003ccode\u003eobject\u003c/code\u003e |  | 验证字段的规则 |\n| [options.messages] | \u003ccode\u003eobject\u003c/code\u003e |  | 验证字段错误的提示信息 |\n| [options.onMessage] | \u003ccode\u003efunction\u003c/code\u003e |  | 错误信息显示方式\u003cbr\u003e默认会自动检测环境。小程序默认使用`showToast`\u003cbr\u003e普通web浏览器默认使用`alert`\u003cbr\u003eNodejs端不做处理建议自己配置，[详情](#wevalidatoronmessage) |\n| [options.multiCheck] | \u003ccode\u003eboolean\u003c/code\u003e | `false` | 是否校验多个字段\u003cbr\u003e需要一次校验多个字段并显示错误信息时使用，[详情](#多个字段同时校验并显示错误) |\n\n\u003cdetails\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```javascript\nconst WeValidator = require('we-validator')\n\nnew WeValidator({\n    rules: {\n        username: {\n            required: true\n        },\n        phoneno: {\n            required: true,\n            mobile: true\n        }\n    },\n    messages: {\n        username: {\n            required: '请输入用户名'\n        },\n        phoneno: {\n            required: '请输入手机号',\n            mobile: '手机号格式不正确'\n        }\n    }\n})\n```\n\n\u003c/details\u003e\n\n### .checkData(data, onMessage)\n\u003e 校验数据，会显示错误信息，校验所有字段规则\n\n**返回**: \u003ccode\u003eboolean\u003c/code\u003e\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| data | \u003ccode\u003eobject\u003c/code\u003e |  | 需要校验的表单数据 |\n| onMessage | \u003ccode\u003efunction\u003c/code\u003e |  | 自定义错误信息提示，[详情](#wevalidatoronmessage) |\n\n### .checkFields(data, fields, onMessage)\n\u003e 校验数据，会显示错误信息，只校验对应的字段，[参考](./example/mpvue/src/pages/index/index.vue)。\n\n通常用于单独校验一个或多个字段规则\n\n**返回**: \u003ccode\u003eboolean\u003c/code\u003e\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| data | \u003ccode\u003eobject\u003c/code\u003e |  | 需要校验的表单数据 |\n| fields | \u003ccode\u003earray\u003c/code\u003e |  | 校验的字段规则，必填\u003cbr\u003e例如：\u003cbr\u003e`['phoneNo']` 只校验此字段的所有规则\u003cbr\u003e`['phoneNo:required']` 只校验此字段的 `required` 规则\u003cbr\u003e`['phoneNo:required,mobile']` 只校验此字段的 `required` 和 `mobile` 规则\u003cbr\u003e`['phoneNo', 'code']` 只校验这两个字段的所有规则 |\n| onMessage | \u003ccode\u003efunction\u003c/code\u003e |  | 自定义错误信息提示，[详情](#wevalidatoronmessage) |\n\n### .isValid(data, fields)\n\u003e 校验数据是否有效，不会提示错误信息\n\n使用场景例如：表单中某些字段校验通过按钮才可点击的场景，[参考](./example/mpvue/src/pages/index/index.vue)。\n\n**返回**: \u003ccode\u003eboolean\u003c/code\u003e\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| data | \u003ccode\u003eobject\u003c/code\u003e |  | 需要校验的表单数据 |\n| fields | \u003ccode\u003earray\u003c/code\u003e |  | 校验的字段\u003cbr\u003e不传，默认校验所有字段规则\u003cbr\u003e如果有，只校验对应的字段规则\u003cbr\u003e配置方法同 `.checkFields(data, fields)` |\n\n### .addRules(options)\n\u003e 动态添加字段校验，[参考](./example/complex/index.html#L235)\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| options | \u003ccode\u003eobject\u003c/code\u003e |  | 同 `new WeValidator(options)` ，[详情](#new-wevalidatoroptions) |\n\n\n\u003cdetails\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```javascript\nconst WeValidator = require('we-validator')\n\nconst validatorInstance = new WeValidator({\n    rules: {\n        username: {\n            required: true\n        }\n    },\n    messages: {\n        username: {\n            required: '请输入用户名'\n        }\n    }\n})\n\n// 动态添加校验\nvalidatorInstance.addRules({\n    rules: {\n        phoneno: {\n            required: true,\n            mobile: true\n        }\n    },\n    messages: {\n        phoneno: {\n            required: '请输入手机号',\n            mobile: '手机号格式不正确'\n        }\n    }\n})\n```\n\n\u003c/details\u003e\n\n### .removeRules(fields)\n\u003e 动态移除字段校验，[参考](./example/complex/index.html#L235)\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| fields | \u003ccode\u003earray\u003c/code\u003e |  | 需要移除校验的表单字段 |\n\n```javascript\nvalidatorInstance.removeRules(['username'])\n```\n\n## WeValidator.addRule(ruleName, ruleOption)\n\u003e 静态方法：添加自定义规则，[参考](./example/complex/index.html)\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| ruleName | \u003ccode\u003estring\u003c/code\u003e |  | 规则名称 |\n| ruleOption | \u003ccode\u003eobject\u003c/code\u003e |  | 规则配置 |\n| [ruleOption.message] | \u003ccode\u003estring\u003c/code\u003e |  | 默认错误信息文字\u003cbr\u003e可以动态插入参数，例如 `请输入长度在 {0} 到 {1} 之间的字符`，[参考](./src/rules.js#L127) |\n| [ruleOption.rule] | \u003ccode\u003efunction\\|regexp\u003c/code\u003e |  | 规则校验函数，需要返回一个 `boolean`。\u003cbr\u003e也可以直接写一个正则表达式（如果只是正则类型的校验）。 |\n\n\u003cdetails\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```javascript\nconst WeValidator = require('we-validator')\n\n// 添加自定义规则（这两种写法一样）\nWeValidator.addRule('theRuleName', {\n  message: '默认错误信息文字',\n  rule(value, param){\n    return /\\d/.test(value)\n  }\n})\nWeValidator.addRule('theRuleName', {\n  message: '默认错误信息文字',\n  rule: /\\d/\n})\n\n// 使用方式一，实例化\nnew WeValidator({\n    rules: {\n        field1: {\n            theRuleName: true\n        }\n    },\n    messages: {\n        field1: {\n            theRuleName: '提示信息'\n        }\n    }\n})\n\n// 使用方式二，调用函数\nWeValidator.checkValue('theRuleName', 'str')\n```\n\n\u003c/details\u003e\n\n\n## WeValidator.checkValue(ruleName, value, param)\n\u003e 静态方法：函数校验\n\n| 参数 | 类型 | 默认值 | 描述 |\n| --- | --- | --- | --- |\n| ruleName | \u003ccode\u003estring\u003c/code\u003e |  | 规则名称 |\n| value | \u003ccode\u003estring\u003c/code\u003e |  | 需要校验的内容 |\n| param | \u003ccode\u003eany\u003c/code\u003e |  | 传递给规则参数 |\n\n支持所有[默认支持的规则](#默认支持的规则)，也支持自定义的规则。\n\n```javascript\n // 必填\nlet b1 = WeValidator.checkValue('required', 'str') // true\n\n// 不能小于6的数字\nlet b2 = WeValidator.checkValue('min', 'str', 6) // false\n\n// 大于2小于5的数字\nlet b3 = WeValidator.checkValue('range', 'str', [2, 5]) // false\n```\n\n## WeValidator.onMessage\n\u003e 自定义错误信息提示\n\n可以全局配置一个，也可以单独配置，非常灵活。\n\n优先级是：`.checkData(data, onMessage)` \u003e `new WeValidator({ onMessage })` \u003e `WeValidator.onMessage` \u003e 默认检测\n\n默认的消息提示方式：小程序默认使用`showToast`，浏览器默认使用`alert`，Nodejs端无处理建议自己配置。\n\n\u003cdetails\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```javascript\nconst WeValidator = require('we-validator')\n\n// 1、全局配置\nWeValidator.onMessage = function(data){\n    /*\n    data 参数\n    {\n        msg, // 提示文字\n        name, // 表单控件的 name\n        value, // 表单控件的值\n        param // rules 验证字段传递的参数\n    }\n    */\n}\n\n// 2、实例化配置\nnew WeValidator({\n    rules: {},\n    message: {},\n    onMessage: function(data){\n        alert(data.msg)\n    }\n})\n\n// 3、验证的时候配置 onMessage（nodejs端校验可以使用此方式）\nif(!obj.checkData(formData, onMessage)) return\n\nfunction onMessage(data){\n  alert(data.msg)\n}\n```\n\n\u003c/details\u003e\n\n### 多个字段同时校验并显示错误\n使用场景如下，**注意：当`multiCheck`为`true`时，建议使用自定义`onMessage`**，[参考](./example/web/index.html)\n\n![we-validator](./assets/demo_multi.png)\n\n\u003cdetails\u003e\n\u003csummary\u003e点击查看栗子 :chestnut: \u003c/summary\u003e\n\n```javascript\nvar validatorInstance = new WeValidator({\n  multiCheck: true,\n  onMessage: function(data){\n    console.log(data);\n    \n    // 根据自己的项目去处理，控制错误信息的显示\n  },\n  rules: {\n      username: {\n          required: true\n      },\n      phoneno: {\n          required: true,\n          mobile: true\n      }\n  },\n  messages: {\n      username: {\n          required: '请输入用户名'\n      },\n      phoneno: {\n          required: '请输入手机号',\n          mobile: '手机号格式不正确'\n      }\n  }\n});\n```\n\n\u003c/details\u003e\n\n## Changelog\n[查看更新日志](./CHANGELOG.md)\n\n## Issues\n如果您在使用过程中发现 Bug，或者有好的建议，欢迎[报告问题](https://github.com/ChanceYu/we-validator/issues)。\n\n## License\n\n[![license][img-mit]][url-mit]\n\nCopyright (c) 2019 ChanceYu\n\n[url-github]: https://github.com/ChanceYu/we-validator\n[url-npm]: https://www.npmjs.com/package/we-validator\n[url-travis]: https://travis-ci.org/ChanceYu/we-validator\n[url-mit]: https://opensource.org/licenses/mit-license.php\n\n[img-npm]: https://nodei.co/npm/we-validator.png?compact=true\n[img-travis]: https://travis-ci.org/ChanceYu/we-validator.svg?branch=master\n[img-javascript]: https://img.shields.io/badge/language-JavaScript-brightgreen.svg\n[img-mit]: https://img.shields.io/npm/l/we-validator.svg\n\n","funding_links":[],"categories":["组件"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FChanceYu%2Fwe-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FChanceYu%2Fwe-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FChanceYu%2Fwe-validator/lists"}