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-input-check

v0.2.0

Published

Template Based Validation Framework for Vue.js.

Downloads

22

Readme

vue-input-check

Template Based Validation Framework for Vue.js.

如何使用

首先,在你的vue项目中进行安装:

npm install --save vue-input-check

安装完成以后引入:

import inputCheck from 'vue-input-check';

接着根据不同的vue版本注册:

vue2.js

import Vue from 'vue';
Vue.use(inputCheck);

vue3.js+

import { createApp } from 'vue';

let app = createApp(App);

app.use(inputCheck, {
    instance: app
});

然后,我们就可以在表单中使用了:

<form autocomplete="off" novalidate>
    <input v-model='key' name='输入框名称' v-input-check='[key,"validate-express"]'/>
    <!-- 可以有任意多的输入框 -->
</form>

如你所见,上述的v-input-check就是我们对每个输入框定义规则的地方,值是一个数组,第一个值就是输入框的v-model,第二个值是一个字符串,语法如下:

validate-express="val1:param1:param2|val2|valu3:param1"

不同的规则使用|分割,需要传递参数的规则的参数通过:分割。我们来看几个例子:

  • v-input-check='[key,"required|maxLength:10|regexp:^\\d{1,5}$"]'
  • v-input-check='[key,"required"]'

目前可选的内置规则如下:

  • required:boolean:表示必输,有一个可选参数,表示是否必输,默认true
  • maxLength:num:最大长度
  • minLength:num:最小长度
  • regexp:str:正则表达式

页面的规则定义好了以后,你有两种方式获取校验的结果。

1.JS的方式

直接使用下列方法启动检查即可:

this.$validateCheck(formnode, callback, errorback);

此对象包含三个参数:

  • formnode:需要校验的表单结点,必输
  • callback:表单合法回调,可选
  • errorback:表单非法回调,可选

此外,错误回调有一个形参,数据格式为:

{
    "$el":错误的输入框结点
    "$error":当前输入框的第一个错误提示信息
}

2.HTML的方式

提供这种方式的目的是为了可以在页面实时反馈当前表单的输入情况。

首先,在表单上,你可以通过判断class包含v-valid或者v-invalid来判断表单是否合法。

同样的,添加指令v-input-check的地方同样可以这样判断该处是否合法,而对于更具体的错误细节,比如必输非法,class就会像这样v-invalid-required v-invalid

自定义校验规则

在大部分情况下,我们还可能需要添加新的校验规则,毕竟默认的往往不足以满足所有业务情况:

// 如果是vue3+,Vue改成app即可
Vue.use(inputCheck, {

    // 如果是vue3+,别忘了这个配置项
    instance: app,

    // 自定义校验规则
    validate: [{

        // 规则的名称
        name: "XXX",

        // 校验方法,返回true表示合法,false表示非法
        // 需要注意的是,这个函数除了el和val一定存在外,余下的参数是使用的时候通过```:```分割传递的,可以有任意多个
        // 比如:``` required:true|phone:parm1:param2 ```
        test: function (el, val, ...) {
            return true|false;
        },

        // 非法提示信息,应该返回一个字符串
        message: function (el, name) {
            return "XXX";
        }
    },
    // 校验规则可以有多条
    ......
    ]

});

开源协议

MIT

Copyright (c) 2021-2022 hai2007 走一步,再走一步。