@34fame/validator 中文文档教程
@34fame/validator
React 的表单字段验证库
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
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.
@34fame/validator
Form field validation library for React
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
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.