Git Product home page Git Product logo

validator.js's Introduction

validator.js Build Status NPM version

一个类似laravel的js验证模块.

安装

  1. 在Nodejs使用
$ npm install mod-validator
  1. 浏览器里使用

下载本项目lib/validator.js 到您的项目目录,引入即可:

<script src="path/to/validator.js"></script>

基本用法

var Validator = require("validator");// 浏览器就不用这句了,Validator是全局变量
var rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
}

var data = {
  username: 'test',
  password: '123456',
}

var v = Validator.make(data, rules); // 或者: var  v = new Validator(data, rules)

if(v.fails()) {
  console.log(v.messages());
  //or
  console.log(v.errors());
}

定义验证规则

  • "|" 分隔的字符串形式
var rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
  email: 'email'
}
  • 数组形式
var rules = {
  username: ['required', 'min:5'],
  password: ['required', ['confirmed'], ['min:6'], ['max:16'],
  email: ['required', 'email']
}

API

  • 获取验证结果

    • 语法:
      • {Boolean} Validator.passes() 是否通过
      • {Boolean} Validator.fails() 是否验证失败
    • 举例:
     var rules = {
       username: 'required|min:5',
       password: 'required|confirmed|min:6|max:16',
     }
    
     var data = {
       username: 'test',
       password: '123456',
     }
    
     v = validator.make(data, rules);
    
     if (v.passes()) {
       // 如果全部通过验证
     }
     // 或者
     if (v.fails()) {
       // 如果没通过验证
     }
  • 自定义错误消息

    • 语法:{Void} Validator.mergeMessage(attribute [, message])
      • Validator.mergeMessage({attributeName, message})
      • Validator.mergeMessage('attributeName', 'message')
    • 属性替换:在消息字符串用使用:attribute 作为属性名占位符。
    • 举例:
    var messages = {
       required: ':attribute 不能为空.',
       // ...
    }
    ...
    v = validator.make(data, rules);
    v.mergeMessage(messages);
    if(v.fails()) {
      console.log(v.errors());
    }
    
    //------------------------------------------
    
    v.mergeMessage({required: ':attribute 不能为空.'})
    //以上用法等同于:
    v.mergeMessage('required', ':attribute 不能为空.');
  • 自定义属性别名

    • 语法:{Void} Validator.mergeAttribute(attribute [, alias])
      • Validator.mergeAttribute({attributeName, alias})
      • Validator.mergeAttribute('attributeName', 'alias')
    • 举例:
    var attributes = {
       username: '用户名',
       password: '密码'
       //...
    }
    v = validator.make(data, rules);
    v.mergeAttribute(attributes);
    
    if(v.fails()) {
      console.log(v.messages());
    }
    
    //------------------------------------------
    
    v.mergeAttribute({username: '用户名'})
    // 以上等同于:
    v.mergeAttribute('username', '用户名');

#License

MIT

validator.js's People

Contributors

overtrue avatar

Watchers

James Cloos avatar Daci avatar

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.