npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-form-check

v1.0.9

Published

表单验证

Downloads

21

Readme

##vue-form-check (基于vue的表单验证)

安装

// 安装
npm i vue-form-check -S

引用

// 引用(eg. 在工程的main.js下)
import vueFormCheck from 'vue-form-check'
Vue.use(vueFormCheck)

调用

this.$checkForm(current, config)
@params
current 是当前校验对象
config  是校验规则对象

config.alias     别名
config.type      配置项数据类型
config.required  是否必填、非空验证
    常用不同类型初始化为空条件
    1、number 类型: Infinity, -Infinity
    2、array 类型: []
    3、string 类型: ''
    4、object 类型: {}
    5、function 类型: new Function()

    // null 和 undefined 赋值之后就不是相应的类型,会不通过,不可用
    6、undefined 类型: undefined
    7、null 类型: null
    // 特殊情况,可通过将 boolean, regexp 转换为 string 类型进行验证
    8、boolean 类型: 初始化默认为false,无法触发非空检验
    9、regexp 类型: 初始化默认为/(?:)/,无法触发非空检验

config.rule      正则校验
config.depend    先决条件(省事可以在callback里直接判断,推荐写,true校验本项;false不校验本项)
config.callback  灵活校验(rule同时出现,只处理callback,参数是当前值,true校验通过;false校验不通过)

@return object 对象
不通过的话    {alias: '电话', type: 'rule'}   alias是配置的别名,type可以是['type'|'required'|'rule']
校验通过的话  {} 空对象

ps. 验证表单可以写在mixin里,这里简单处理直接写在组件里了

Component

// 使用例子
new Vue({
  data() {
    return {
      params: {
        id: '1234',
        person: {
            name: 'jackie',
            age: '27',
            phone: '18266666666',
            home: ['罗湖区田心村']
        }
      }
    }
  },
  methods: {
    submit() {
      //...
      console.log('submit success');
    },
    check() {
      let obj = this.$checkForm(this.params, {
            id: {
              alias: 'id',
              type: 'string'
            },
            // 必填校验
            'person.name': {
                alias: '学校',
                type: 'string',
                required: true
            },
            // 正则校验
            'person.phone': {
                alias: '电话',
                type: 'string',
                rule: /^1[345678][0-9]{9}$/
            },
            // 灵活校验,如数值、日期区间验证
            'person.age': {
                alias: '年龄',
                callback(value) {
                    if (value < 30 && value > 18) {
                        return true;
                    }
                    return false;
                }
            },
            // 先决校验,如果电话等于以下,校验地址信息
            'person.home': {
                alias: '方向',
                type: 'array',
                required: true,
                depend() {
                    if (this.params.person.phone === '18210517463') {
                      return true;
                    }
                    return false;
                }
          }
        });
        const length = Object.keys(obj).length;
        if (length === 0) {
            return this.submit();
        }
        switch (obj.type) {
            case 'type':
                this.$alert(`${obj.alias}的类型定义错误`, '提示');
                break;
            case 'required':
                this.$alert(`${obj.alias}是必填项`, '提示');
                break;
            case 'rule':
                this.$alert(`${obj.alias}的输入不符合规范`, '提示');
                break;
            default:
                break;
        }
    }
  }
});