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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jypj/validate-control

v1.0.4

Published

一个简单校验器,用于验证传入的数据是否符合要求

Readme

validate-control

一个简简单单的校验器

使用说明

使用前需要先new一个实例出来,然后使用validate函数进行数据校验,实例内部有一个pass的属性,当这个属性为true时,即校验通过。 validate的函数签名如下

 /**
 * 校验函数,传入rule 以及 错误提示信息即可
 * @param {string} rule - 校验规则,内部已集成一些校验可以调用
 * @param {string} error_msg - 错误提示信息
 * @param {type} props - 当内部集成的函数需要额外参数,通过此参数传入
 * @return {ValidateControl} 返回实例
 */
validate(rule = null, error_msg = null,props) :  class ValidateControl()

使用例子

校验单个值

const validateControl = new ValidateControl('13022222222')
validateControl.validate('isMobile','请输入正确的手机号码')

// 校验通过
if (validateControl.pass){
    // ...
}

校验多个值

如果需要校验多个数据的情况下,插件内部有一个value函数,在这里可以重新设置插件内部的值。 使用例子如下:

const validateControl = new ValidateControl('13022222222')
validateControl.validate('isMobile','请输入正确的手机号码')
validateControl.value('130xxxxxxxx')
validateControl.validate('isMobile','请输入正确的手机号码')

// 校验通过
if (validateControl.pass){
    // ...
}

在这种多数据校验的情况下,如果上一个数值不通过,则不会执行下面的校验,会直接返回错误

链式调用

const validateControl = new ValidateControl('13022222222')
    .validate('isMobile','请输入正确的手机号码')
    .value('130xxxxxxxx')
    .validate('isMobile','请输入正确的手机号码')

// 校验通过
if (validateControl.pass){
    // ...
}

需要额外传输参数时

const validateControl = new ValidateControl('13022222222')
    .validate('isRegExp','请输入正确的手机号码',/^(13[0-9]{1}|14[5|7|9]{1}|15[0-3|5-9]{1}|166|17[0-3|5-8]{1}|18[0-9]{1}|19[8-9]{1}){1}\d{8}$/)
   

// 校验通过
if (validateControl.pass){
    // ...
}

内部集成的校验规则

| 调用名 | 简介 | | ------------ |-----------------------| | isMobile | 是否为手机号码 | | isRequired | 是否为空,支持对象,数组,数字为0的也为空 | | isIdCard | 是否为身份证号码 | | isRegExp | 自定义正则校验 | | isMoneyFloat | 是否为最多两位小数点的金额 | | isExternal | 是否为外链(携带http / https) | | isChinese | 是否全为中文 | | isEmail | 是否为邮箱 |