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

verification-puge

v1.0.3

Published

轻量级的JavaScript表单验证,字符串验证。没有依赖,支持UMD,~3kb。

Downloads

19

Readme

安装使用

模块

在应用中引用 validator.min.js 文件

# npm 安装
$ npm install verification-puge --save
# yarn 安装
$ yarn add verification-puge

.js 文件中调用

// 字符串验证
var validator = require('verification-puge');
var v = new validator();
v.isEmail('[email protected]');
v.isIp('192.168.23.3');
v.isFax('');

// 表单验证
var a = new validator('example_form',[
    {...}
],function(obj,evt){
    if(obj.errors){
        // 判断是否错误
    }
})

客户端使用

在应用中引用 validator.js 文件, 手动下载并链接HTML中的 validator.js

<script type="text/javascript" src="./validator.js"></script>

在JS中使用方法。

<script type="text/javascript">
  var v = new Validator();
  v.isEmail('[email protected]');
  v.isIp('192.168.23.3');
</script>

说明文档

new Validator(formName, option, callback)

formName

formName 是标签中<form> 中的 id 或者 name 的值,如上面的example_form

option

  • name -> input 中 name 对应的值

  • display -> 验证错误要提示的文字 {{这个中间是name对应的值}}

  • rules -> 一个或多个规则(中间用|间隔)

    • is_email -> 验证合法邮箱
    • is_ip -> 验证合法 ip 地址
    • is_fax -> 验证传真
    • is_tel -> 验证座机
    • is_phone -> 验证手机
    • is_url -> 验证URL
    • is_money -> 金额格式验证
    • is_english -> 字母验证⚠️
    • is_chinese -> 中文验证
    • is_percent -> 验证百分比⚠️
    • required -> 是否为必填
    • max_length -> 最大字符长度
    • min_length -> 最小字符长度
    • noSpecial -> 判断是否没有特殊字符
    • same(field) -> 指定字段内容是否相同
    • different(field) -> 拒绝与某个字段相等,比如登录密码与交易密码情况
    • contains(field) -> 直接判断字符串是否相等
    • accepted(field) -> 用于服务条款,是否同意时相当有用,不限制checkbox与radio,有可能submit button直接附带value情况
{
    //name 字段
    name: 'email',
    display:"你输入的不{{email}}是合法邮箱|不能为空|太长|太短",
    // 验证条件
    rules: 'is_email|max_length(12)'
    // rules: 'valid_email|required|max_length(12)|min_length(2)'
}

自定义正则

自定义正则,以regexp_开始

{
  //name 字段
  name: 'sex',
  // 对应下面验证提示信息
  display:"请你选择性别{{sex}}|请输入数字",
  //自定义正则`regexp_num`
  regexp_num:/^[0-9]+$/,
  // 验证条件,包括应用自定义正则`regexp_num`
  rules: 'required|regexp_num'
}

callback

var validator = new Validator('example_form',[
    {...},{...}
],function(obj,evt){
    //obj = {
    //  callback:(error, evt, handles)
    //  errors:Array[2]
    //  fields:Object
    //  form:form#example_form
    //  handles:Object
    //  isCallback:true
    //  isEmail:(field)
    //  isFax:(field)
    //  isIp:(field)
    //  isPhone:(field)
    //  isTel:(field)
    //  isUrl:(field)
    //  maxLength:(field, length)
    //  minLength:(field, length)
    //  required:(field)
    //} 
    if(obj.errors.length>0){
        // 判断是否错误
    }
})

特殊情况直接提交

var validator = new Validator('example_form',[
    {...},{...}
],function(obj,evt){
    //obj = {
    //} 
    if(obj.errors.length>0){
        // 判断是否错误
    }
})
validator.passes().form.submit();

例子

字符串验证

var v = new Validator();
v.isEmail('[email protected]'); // -> 验证合法邮箱  |=> 返回布尔值
v.isIp('192.168.23.3'); // -> 验证合法 ip 地址  |=> 返回布尔值
v.isFax(''); // -> 验证传真  |=> 返回布尔值
v.isPhone('13622667263'); // -> 验证手机  |=> 返回布尔值
v.isTel('021-324234-234'); // -> 验证座机  |=> 返回布尔值
v.isUrl('http://JSLite.io'); // -> 验证URL  |=> 返回布尔值
v.maxLength('JSLite',12); // -> 最大长度  |=> 返回布尔值
v.minLength('JSLite',3); // -> 最小长度  |=> 返回布尔值
v.required('23'); // -> 是否为必填(是否为空)  |=> 返回布尔值

表单中验证

点击按submit按钮验证

var validator = new Validator('example_form',[
    {
        //name 字段
        name: 'email',
        display:"你输入的不{{email}}是合法邮箱|不能为空|太长|太短",
        // 验证条件
        rules: 'is_email|max_length(12)'
        // rules: 'valid_email|required|max_length(12)|min_length(2)'
    },{
        //name 字段
        name: 'sex',
        display:"请你选择性别{{sex}}",
        // 验证条件
        rules: 'required'
    }
],function(obj,evt){
    if(obj.errors){
        // 判断是否错误
    }
})

没有submit验证

var validator = new Validator('example_form',[
    {
        //name 字段
        name: 'email',
        display:"你输入的不{{email}}是合法邮箱|不能为空|太长|太短",
        // 验证条件
        rules: 'is_email|max_length(12)'
        // rules: 'valid_email|required|max_length(12)|min_length(2)'
    },{
        //name 字段
        name: 'sex',
        display:"请你选择性别{{sex}}|请输入数字",
        regexp_num:/^[0-9]+$/,
        // 验证条件
        rules: 'required|regexp_num'
    }
],function(obj,evt){
    if(obj.errors){
        // 判断是否错误
    }
})
validator.validate()

参考

借鉴这些优秀的库,撸出此玩意儿。