@34fame/validator 中文文档教程

发布于 5年前 浏览 30 项目主页 更新于 3年前

@34fame/validator

React 的表单字段验证库

NPMJavaScript Style Guide

validator 是一个简单易用但非常强大的 React 字段验证库。 受到 Dana Janoskova 和她我如何尝试验证 React 表单(带钩子) 的启发 文章

Install

npm install --save @34fame/validator
yarn add @34fame/validator

Usage

import React from 'react'
import useForm from '@34fame/validator'

const App = () => {
   const { values, setValues, useInput, isValid } = useForm(init)

   const handleOnSubmit = e => {
      e.preventDefault()
      console.log(values)
   }

   return (
      <React.Fragment>
         <form onSubmit={() => handleOnSubmit()}>
            Username:<br>
            <input
               type="text"
               {...useInput('username', {
                  isRequired: true,
                  isLength: {
                     min: 3
                  },
                  isAlphanumeric: {
                     locale: ['en-US']
                  }
               })}
            />
            <br>
            Password:<br>
            <input
               type="password"
               {...useInput('password', {
                  isRequired: true,
                  isLength: {
                     min: 8,
                     max: 20
                  }
               })}
            />
            <br>
            Verify Password:<br>
            <input
               type="password"
               {...useInput('verify', {
                  isRequired: true,
                  equals: values.password
               })}
            />
            <input disabled={!isValid} type="submit" value="Submit">
         </form>
      </React.Fragment>
   )
}

export default App

Examples Site

您可以在此处

Contributors

Credits

validatorjs/validator.js

DJanoskova/React-validator-demo

License (MIT)

Copyright (c) 2020 Troy Moreland <troy@34fame.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Troy Moreland,34 Fame 的创始人

@34fame/validator

Form field validation library for React

NPMJavaScript Style Guide

validator is a simple to use yet very powerful field validation library for React. Inspired by the efforts of Dana Janoskova and her How I tried to validate React forms (with hook) article.

Install

npm install --save @34fame/validator
yarn add @34fame/validator

Usage

import React from 'react'
import useForm from '@34fame/validator'

const App = () => {
   const { values, setValues, useInput, isValid } = useForm(init)

   const handleOnSubmit = e => {
      e.preventDefault()
      console.log(values)
   }

   return (
      <React.Fragment>
         <form onSubmit={() => handleOnSubmit()}>
            Username:<br>
            <input
               type="text"
               {...useInput('username', {
                  isRequired: true,
                  isLength: {
                     min: 3
                  },
                  isAlphanumeric: {
                     locale: ['en-US']
                  }
               })}
            />
            <br>
            Password:<br>
            <input
               type="password"
               {...useInput('password', {
                  isRequired: true,
                  isLength: {
                     min: 8,
                     max: 20
                  }
               })}
            />
            <br>
            Verify Password:<br>
            <input
               type="password"
               {...useInput('verify', {
                  isRequired: true,
                  equals: values.password
               })}
            />
            <input disabled={!isValid} type="submit" value="Submit">
         </form>
      </React.Fragment>
   )
}

export default App

Examples Site

You can see validator at work here

Contributors

Credits

validatorjs/validator.js

DJanoskova/React-validator-demo

License (MIT)

Copyright (c) 2020 Troy Moreland <troy@34fame.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Troy Moreland, Founder of 34 Fame

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文