# 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 === [](https://jaywcjlove.github.io/#/sponsor) [](https://github.com/jaywcjlove/validator.js/actions/workflows/ci.yml) [](https://www.npmjs.com/package/validator.tool) [](https://bundlephobia.com/package/validator.tool) [](https://www.npmjs.com/package/validator.tool) [](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) [](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 ( ); } ``` ```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 ( ); } ``` ### Used in the React App [](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 ( ); } ``` ### [Support React Hook](https://github.com/jaywcjlove/validator.js/tree/master/packages/hook/README.md) [](https://bundlephobia.com/package/@validator.tool/hook) [](https://www.npmjs.com/package/@validator.tool/hook) [](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 `{validator.current.message('email', data.email, { validate: (val) => !isEmail(val) ? `The ${val} must be a valid email address.` : '' })}
); } ``` ### [Zod](https://github.com/vriad/zod) TypeScript-first schema validation with static type inference [](https://bundlephobia.com/package/zod) ## Development To develop, Install dependencies, Get the code: ```bash $ git https://github.com/jaywcjlove/validator.js.git $ cd validator.js # Into the directory $ npm install # or yarn install $ npm install --workspaces # Install monorepo dependency ``` To develop, run the self-reloading build: ```bash $ npm run lib:watch # Monitor the compiled package `validator.tool` $ npm run hook:watch # Monitor the compiled package `@validator.tool/hook` ``` Run Document Website Environment. ```bash $ npm run start ``` To contribute, please fork Hotkeys.js, add your patch and tests for it (in the `