# validator-js **Repository Path**: mirrors/validator-js ## Basic Information - **Project Name**: validator-js - **Description**: 轻量级的JavaScript表单验证,字符串验证 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: https://www.oschina.net/p/validator-js-2 - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-06-23 - **Last Updated**: 2026-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README validator.js === [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) [![Build & Deploy](https://github.com/jaywcjlove/validator.js/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/validator.js/actions/workflows/ci.yml) [![No Dependencies](https://shields.io/badge/no-dependencies-green?style=flat)](https://www.npmjs.com/package/validator.tool) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/validator.tool)](https://bundlephobia.com/package/validator.tool) [![npm version](https://img.shields.io/npm/v/validator.tool.svg)](https://www.npmjs.com/package/validator.tool) [![Coverage Status](https://jaywcjlove.github.io/validator.js/coverage/badges.svg)](https://jaywcjlove.github.io/validator.js/coverage/lcov-report) Lightweight JavaScript form validation, that had minimal configuration and felt natural to use. No dependencies, support UMD. > ⚠️ The [`v1`](https://raw.githack.com/jaywcjlove/validator.js/v1-doc/index.html) version document preview is [here](https://raw.githack.com/jaywcjlove/validator.js/v1-doc/index.html). [Install](#install) · [Usage](#usage) · [React](#used-in-the-react-app) · [Hook](#support-react-hook) · [React Native](#used-in-the-react-native-app) · [Form](#used-in-the-browser-client) · [API](#api) · [npm](http://npm.im/validator.tool) · [License](#license) ## Used in the browser client Refer to the [`validator.min.js`](https://unpkg.com/validator.tool/) file in the application, manually download and link [validator.min.js](https://github.com/jaywcjlove/validator.js/tree/master/packages/core/dist) in HTML. ```html ``` It can also be downloaded via [UNPKG](https://unpkg.com/validator.tool/): CDN: [UNPKG](https://unpkg.com/validator.tool/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/validator.tool/) | [Githack](https://raw.githack.com/jaywcjlove/validator.js/master/packages/core/dist/validator.min.js) | [Statically](https://cdn.statically.io/gh/jaywcjlove/validator.js/master/packages/core/dist/validator.min.js) | [bundle.run](https://bundle.run/validator.tool) [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/quirky-bohr-m75fx?fontsize=14&theme=dark) ```html
``` ## Install ```bash $ npm install validator.tool --save # Or $ npm install @validator.tool/hook --save ``` ## Usage ```jsx import { useState, useRef } from 'react'; import Validator from 'validator.tool'; function Example() { const [data, setData] = useState({ email: 'kennyiseeyou@gmail.com' }); const validator = useRef( new Validator({ initValues: { ...data }, }) ); return (
{validator.current.message('email', data.email, { validate: (val) => !/^[A-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(val) ? `The ${val} must be a valid email address.` : '' })}
); } ``` ```jsx import { useState } from 'react'; import { useValidator } from '@validator.tool/hook'; function Example() { const [data, setData] = useState({ email: 'kennyiseeyou@gmail.com' }); const { validator } = useValidator({ initValues: { ...data }, }); return (
{validator.message('email', data.email, { validate: (val) => !/^[A-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(val) ? `The ${val} must be a valid email address.` : '' })}
); } ``` ### Used in the React App [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/used-in-the-react-app-andras-dbzbz-forked-7li45?fontsize=14&hidenavigation=1&theme=dark) ```jsx import { useRef, useState } from 'react'; import { useValidator } from '@validator.tool/hook'; function Demo() { const [data, setData] = useState({ email: 'kennyiseeyou@gmail.com' }); const { validator, handleReset, handleSubmit } = useValidator({ initValues: data, validate: (value, values, field) => { if (field === 'password' && !value) { return 'Required!'; } } }); const onSubmit = (value: Values) => { console.log('value', value) } const onReset = (value: Values) => { setData({ ...value }); } function handleChange(env) { const target = env.target; const value = target.type === "checkbox" ? target.checked : target.value; const name = target.name; setData({ ...data, [name]: value }); } return (
{validator.message('email', data.email, { validate: (val) => !/^[A-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(val) ? `The ${val} must be a valid email address.` : '' })}
{validator.message('password', data.password)}
); } ``` ### [Support React Hook](https://github.com/jaywcjlove/validator.js/tree/master/packages/hook/README.md) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@validator.tool/hook)](https://bundlephobia.com/package/@validator.tool/hook) [![npm version](https://img.shields.io/npm/v/@validator.tool/hook.svg)](https://www.npmjs.com/package/@validator.tool/hook) [![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/validator-js-support-hook-andras-dbzbz-dbzbz?fontsize=14&hidenavigation=1&theme=dark) ```jsx import { useValidator } from '@validator.tool/hook'; const { validator, forceUpdate } = useValidator({}); ``` ### Used in the React Native App You need to wrap validator with `` Element. [![](https://img.shields.io/badge/style-Expo-green?label=Open%20In&logo=expo&style=flat&color=#333)](https://snack.expo.dev/@jaywcjlove/validatorjs-test) ```jsx import React, { useRef } from 'react'; import { Text, View, Button } from 'react-native'; import { useValidator } from '@validator.tool/hook'; const WelcomeScreen = () => { const [text, onChangeText] = React.useState('Useless Text'); const { validator, forceUpdate } = useValidator({ initValues: { text }, validate: (value = '', values, field) => { if (field === 'username' && value.length > 3) { return '!! username > 3'; } }, }); return ( {validator.message('username', text)}