Giter Club home page Giter Club logo

tiny-validator's Introduction

超轻量级表单验证库 npm npm

Install

npm install tiny-validator

Usage

var validator = new Validator($('form'), {
    config: {
        'test1': {
            required: true,
            valid: /\w{3,}/,
            validTips: '最少输入3个字符'
        },
        'test2': {
            required: true
        }
    },
    handle: function(error, $el) {
        if(error) {
            $el.next().html(error.tips)
        } else {
            $el.next().html('')
        }
    }
})

配置参数

config { Object }

验证规则配置对象

config: {
    //对应input,select或update的name属性
    'name': {
        //是否必须, boolean 
        required: true/false,
        //验证输入值的正则或要执行的验证函数(返回值为boolean)
        valid: /\w{3,}/,
        //required 为 false 时的提示信息,默认为: 该项为必填项
        requiredTips: '请输入 xxx',
        //valid 为 false 时的提示信息,默认为:您的输入不正确
        validTips: 'xxx 无效'
    },
    'name2': ...
}

handle { Function }

对每个验证项验证后的回调函数。

回调的参数为:(error, $el)

API

valid()

根据 config 配置,验证表单,返回所有错误提示信息。

isValid()

判断表单是否有效,返回 boolean。

setTips(name, tips)

主动设置提示信息。

destroy()

销毁表单验证组件实例。

Recent Release

v0.1.3

  • 修正 seajs 的兼容问题。

tiny-validator's People

Contributors

petermu avatar

Stargazers

Noodles avatar  avatar  avatar 穆恩 avatar  avatar  avatar

Watchers

 avatar

Forkers

sunlanlisa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.