npm i react-client-validation
or use CDN
<script src="https://unpkg.com/react-client-validation@1.5.0/dist/index.min.js"></script>
import handleValidation from 'react-client-validation' const loginValidation = [ { index: 'username', condition: [!username], errorMessage: 'User name is not valid!', }, { index: 'password', //change errorObject's format errorFormat: ['invalidPassword'], customCondition: (data, errorReturnArray) => { if (data.password) { errorReturnArray.push(false) } }, }, ] const [isPass, loginErrorObject] = handleValidation({ errorArray: loginValidation, defaultErrorMessage: "input can't be blank", })
console.log(isPass); // boolean, true or false console.log(loginErrorObject); { username: {msg: 'User name is not valid!'}, password: [ invalidPassword: {msg: "input can't be blank"}] //password's value is formatted based on errorFormat from the errorArray }
defaultMessage: string: if errorMessage is not found for the index, then defaultMessage will be applied to its return error message.
dataSource: any (is isRequired if using customCondition)
errorArray: Array (isRequired)
//errorFormat example //only allowed when using customCondition 1. without errorFormat { ..., customCondition: ..., } //returned error object { username: {msg:'User name is not valid!'} } 2. with errorFormat //scenario 1 { ..., errorFormat: ['test'], customCondition: ..., } //returned error object { username: {test: {msg:'User name is not valid!'} } } //scenario 2 // 'index' will return the current index. { ..., errorFormat: ['test', 'index'], customCondition: ..., } //returned error object { username: {test: { 0: { msg: 'User name is not valid!'} } } }
//customCondition example /** when using customCondition, push boolean to the errorReturnArray from customCondition push true if the current condition passed, otherwise push false **/ {..., customCondition: (data, errorReturnArray) => { if (!data.password) { errorReturnArray.push(false) // validation failed, will get fail message from the return error object } } }
Email 0529bill@gmail.com
Released under MIT License.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4