1 Star 0 Fork 75

wangxinwen / validate-framework

forked from MinJieLiu / validate-framework 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#validate-framework

npm version

一款轻量、无依赖的 JavaScript 验证组件

Demo: http://minjieliu.github.io/validate-framework/example

特性

  1. 轻量、无依赖
  2. 相同 name 的表单验证
  3. 动态验证
  4. 兼容 chrome 、firfox 、IE9 +

快速上手

通过 npm 安装

npm install validate-framework --save

基本用法:

<form name="basicForm">
  <div class="form-group">
    <label for="email">邮箱:</label>
    <input class="form-control" id="email" name="email" type="email" placeholder="请输入邮箱" />
  </div>
  <div class="form-group">
    <label for="phone">手机:</label>
    <input class="form-control" id="phone" name="phone" type="text" placeholder="请输入手机号" />
  </div>
  <input class="btn btn-primary" id="submit" type="submit" value="提交" />
</form>
import validateFramework from 'validate-framework';

const validator = new validateFramework({
  formName: 'basicForm',
  fields: {
    email: {
      rules: 'required | isEmail | maxLength(32)',
      messages: "不能为空 | 请输入合法邮箱 | 不能超过 {{param}} 个字符"
    },
    phone: {
      rules: 'isPhone',
      messages: "手机号: {{value}} 不合法"
    }
  },
  callback: function (result, error) {
    // 阻止表单提交
    validator.preventSubmit();
    // do something...
  }
});

// 验证
validator.validate();

说明文档

new validateFramework(options)

options

options (必选)

  • formName (必选) 是 <form> 中的 name 或者 id 的值
  • fields (可选) 表单验证域 rulesmessages 集合,后续可通过 .addMethods(methods).removeMethods(...names) 进行变更
  • errorPlacement (可选) 错误信息位置
  • callback (可选) 表单提交 或 .validate() 调用后触发
  • classNames (可选) 验证正确或错误 class

参数示例

fields

fields: {
  email: {
    rules: 'required | isEmail | maxLength(32)',
    messages: "不能为空 | 请输入合法邮箱 | 不能超过 {{param}} 个字符"
  },
  phone: {
    rules: 'isPhone',
    messages: "手机号: {{value}} 不合法"
  }
}

注: emailphone 为表单 name 属性
rules :(必选) 一个或多个规则(中间用 | 分隔)
messages :(可选) 相对应的错误提示(中间用 | 分隔) {{value}} 为表单中的 value 值, {{param}}maxLength(32) 的参数

errorPlacement

errorPlacement: function (errorEl, fieldEl) {
  // 非 label 、radio 元素
  if (fieldEl.parentNode !== undefined) {
    fieldEl.parentNode.appendChild(errorEl);
  }
},

注: errorEl 为错误信息节点,fieldEl 为验证的表单节点 验证失败后,表单中会添加 valid-error , 错误信息中添加 valid-error-message 类名

callback

callback: function (result, error) {
  // 自定义逻辑
  if (errors) {
      // do something...
  }
}

注: result 验证结果
error 验证失败的错误集合

方法

  • 除返回值为 Boolean 类型的方法都支持链式调用

.validate() 手动验证

返回值为 Boolean
注: 默认使用 submit 按钮提交进行拦截验证,可手动调用 .validate() 调用验证所有定义过的元素

.validateByName(name) 通过 name 验证单个表单域

返回值为 Boolean

.preventSubmit() 阻止表单提交

.addMethods(methods) 自定义验证方法

如:

// checkbox 至少选择两项 方法
validator.addMethods({
  selectLimit: function (field, param) {
    // checkbox 至少选择两项
    var checkedNum = 0;
    for (var i = 0, elLength = field.el.length; i < elLength; i++) {
      if (field.el[i].checked) {
        checkedNum += 1;
      }
    }
    return checkedNum >= param;
  },
});

.addFields(fields) 动态添加 fields 方法

注:通过 .addFields(fields) 来动态新增一个或多个表单验证域

validator.addFields({
  userName: {
    rules: 'required | isRealName',
    messages: "不能为空 | 请输入真实姓名"
  }
});

.removeFields(...names) 移除 fields 方法

// 移除单个
validator.removeFields('userName');
// 移除多个
validator.removeFields('userName', 'email');

内置验证方法

如:

v.isEmail('example@qq.com');
v.isPhone('170111222231');
  • required(param) 验证必填
  • isAbc(param) 验证字母数字下划线
  • isDate(param) 验证日期
  • isDecimal(param) 验证浮点数
  • isEmail(param) 验证邮箱
  • isInteger(param) 验证整数
  • isIp(param) 验证 ip 地址
  • isNumeric(param) 验证自然数
  • isPhone(param) 验证手机
  • isTel(param) 验证座机
  • isUrl(param) 验证URL
  • maxLength(param, length) 最大长度
  • minLength(param, length) 最小长度
  • greaterThan(param1, param2) 多于某个数
  • lessThan(param1, param2) 少于某个数
  • greaterThanDate(date1, date2) 大于某个日期
  • lessThanDate(date1, date2) 小于某个日期

LICENSE

MIT

The MIT License (MIT) Copyright (c) 2016 MinJie Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一款轻量、无依赖的 JavaScript 验证组件 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/wangxinwen/validator.js.git
git@gitee.com:wangxinwen/validator.js.git
wangxinwen
validator.js
validate-framework
master

搜索帮助