react-inputs-validation
React form input validation components.
:tada: For version >= 3.0.0, please update react and react-dom to at least 16.8.6
, since it is rewrited with hooks.
For version >= 1.1.4, please import css manually.
Codesandbox Examples
- Online demo form example playground
- Custom control(when
check: false
) - Custom function(when providing
customFunc
) - Custom function further control(when providing
customFunc
) - Custom locales(when providing
window.REACT_INPUTS_VALIDATION['customErrorMessage']
) - Phone and email validation example(handled with
customFunc
)
Docs Link
Custom Error Message Guid(can be multiple locales)
Phone and email validation example(Since the phone and email validation are no longer handled internally after v1.4.0.)
Installation
Donation
Thanks for donating me a donut! ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄
Browser support
Tested on IE9+ and Chrome and Safari(10.0.3)
Docs
Textbox
Props | Type | Description | Default | |
---|---|---|---|---|
tabIndex | Opt | Str | Num | none | |
id | Opt | Str | "" | |
name | Opt | Str | "" | |
type | Opt | Str | "text" | |
value | Opt | Str | "" | |
disabled | Opt | Bool | false | |
autoComplete | Opt | Str | "on" | |
maxLength | Opt | Str | Num | 524288 | |
placeholder | Opt | Str | "" | |
validate | Opt | Bool | If you have a submit button and trying to validate all the inputs of your form at once, toggle it to true, then it will validate the field and pass the result via the "validationCallback" you provide. | false |
validationCallback | Opt | Func | Return the validation result. | none |
classNameInput | Opt | Str | "" | |
classNameWrapper | Opt | Str | "" | |
classNameContainer | Opt | Str | "" | |
customStyleInput | Opt | Obj | {} | |
customStyleWrapper | Opt | Obj | {} | |
customStyleContainer | Opt | Obj | {} | |
onChange | Req | Func | (val, e) => {}. Will return the value. | (val, e) => {} |
onBlur | Opt | Func | In order to validate the value on blur, you MUST provide a function, even if it is an empty function. Missing this, the validation on blur will not work. | none |
onFocus | Opt | Func | none | |
onClick | Opt | Func | none | |
onKeyUp | Opt | Func | By providing onKeyUp , the component will perform the checking every time when user types. |
none |
validationOption | Opt | obj | validationOption object, see below | {} |
validationOption.name | Opt | Str | To display in the Error message. i.e Please enter your ${name}. | "" |
validationOption.check | Opt | Bool | To determin if you need to validate. | true |
validationOption.required | Opt | Bool | To determin if it is a required field. | true |
validationOption.type | Opt | Str | Validation type, options are ['string', 'number' |
"string" |
validationOption.showMsg | Opt | Bool | To determin display the error message or not. | true |
validationOption.min | Opt | Num | Validation of min length when validationOption['type'] is string, min amount when validationOption['type'] is number. | 0 |
validationOption.max | Opt | Num | Validation of max length when validationOption['type'] is string, max amount when validationOption['type'] is number. | 0 |
validationOption.length | Opt | Num | Validation of exact length of the value. | 0 |
validationOption.compare | Opt | Str | Compare this value to 3 to see if they are equal. | "" |
validationOption.customFunc | Opt | Func | Custom function. Returns true or err message. | none |
validationOption.reg | Opt | Bool | Custom regex. | "" |
validationOption.regMsg | Opt | Str | Custom regex error message. | "" |
validationOption.locale | Opt | Str | For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'. If your are looking for more options, you can take a look at 'window.REACT_INPUTS_VALIDATION' section, which provides the extensibility for your own locale. | "en-US" |
No longer support after v1.4.0. For phone or email address validation please reffer to 'Phone and email validation example' |
||||
validationOption.msgOnError | Opt | Str | Show your custom error message no matter what(except the message from customFunc) when it has error if it is provied. | "" |
validationOption.msgOnSuccess | Opt | Str | Show your custom success message no matter what when it has error if it is provied. | "" |
asyncMsgObj.error | Opt | Bool | (Server response) Backend validation result. | false |
asyncMsgObj.message | Opt | Str | (Server response) Your AJAX message. For instance, provide it when backend returns 'USERNAME ALREADY EXIST' | "" |
asyncMsgObj.showOnError | Opt | Bool | (Server response) Show AJAX error message or not. | true |
asyncMsgObj.showOnSuccess | Opt | Bool | (Server response) Show AJAX success message or not. | false |
Radiobox
Props | Type | Description | Default | |
---|---|---|---|---|
tabIndex | Opt | Str | Num | none | |
id | Req | Str | IMPORTANT if you have multiple Radiobox on the page, id is used to distinguish them for the label 'for' use | "" |
name | Opt | Str | "" | |
value | Opt | Str | "" | |
disabled | Opt | Bool | false | |
validate | Opt | Bool | If you have a submit button and trying to validate all the inputs of your form at once, toggle it to true, then it will validate the field and pass the result via the "validationCallback" you provide. | false |
validationCallback | Opt | Func | Return the validation result. | none |
optionList | Req | Array | [{id: 'teacher', name: 'teacher'}] | [] |
classNameInput | Opt | Str | "" | |
classNameWrapper | Opt | Str | "" | |
classNameContainer | Opt | Str | "" | |
classNameOptionListItem | Opt | Str | "" | |
customStyleInput | Opt | Obj | {} | |
customStyleWrapper | Opt | Obj | {} | |
customStyleContainer | Opt | Obj | {} | |
customStyleOptionListItem | Opt | Obj | {} | |
onBlur | Opt | Func | In order to validate the value on blur, you MUST provide a function, even if it is an empty function. Missing this, the validation on blur will not work. | none |
onChange | Req | Func | (val, e) => {}. Will return the value. | (val, e)=>{} |
onFocus | Opt | Func | none | |
onClick | Opt | Func | none | |
validationOption | Opt | obj | validationOption object, see below | {} |
validationOption.name | Opt | Str | To display in the Error message. i.e Please enter your ${name}. | "" |
validationOption.check | Opt | Bool | To determin if you need to validate. | true |
validationOption.required | Opt | Bool | To determin if it is a required field. | true |
validationOption.showMsg | Opt | Bool | To determin display the error message or not. | true |
validationOption.locale | Opt | Str | For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'. If your are looking for more options, you can take a look at 'window.REACT_INPUTS_VALIDATION' section, which provides the extensibility for your own locale. | "en-US" |
validationOption.msgOnError | Opt | Str | Show your custom error message no matter what when it has error if it is provied. | "" |
validationOption.msgOnSuccess | Opt | Str | Show your custom success message no matter what when it has error if it is provied. | "" |
asyncMsgObj.error | Opt | Bool | (Server response) Backend validation result. | false |
asyncMsgObj.message | Opt | Str | (Server response) Your AJAX message. For instance, provide it when backend returns 'USERNAME ALREADY EXIST' | "" |
asyncMsgObj.showOnError | Opt | Bool | (Server response) Show AJAX error message or not. | true |
asyncMsgObj.showOnSuccess | Opt | Bool | (Server response) Show AJAX success message or not. | false |
Checkbox
Props | Type | Description | Default | |
---|---|---|---|---|
tabIndex | Opt | Str | Num | none | |
id | Opt | Str | "" | |
name | Opt | Str | "" | |
value | Opt | Str | "" | |
checked | Req | Bool | Recommend using the value returned from onChange callback, which is isChecked . |
false |
disabled | Opt | Bool | false | |
validate | Opt | Bool | If you have a submit button and trying to validate all the inputs of your form at once, toggle it to true, then it will validate the field and pass the result via the "validationCallback" you provide. | false |
validationCallback | Opt | Func | Return the validation result. | none |
classNameWrapper | Opt | Str | "" | |
classNameInputBox | Opt | Str | "" | |
classNameContainer | Opt | Str | "" | |
customStyleWrapper | Opt | Obj | {} | |
customStyleInputBox | Opt | Obj | {} | |
customStyleContainer | Opt | Obj | {} | |
onBlur | Opt | Func | In order to validate the value on blur, you MUST provide a function, even if it is an empty function. Missing this, the validation on blur will not work. | none |
onChange | Req | Func | (isChecked, e) => {}. Will return the value. | (val, e)=>{} |
onFocus | Opt | Func | none | |
onClick | Opt | Func | none | |
labelHtml | Req | Html | none | |
validationOption | Opt | obj | validationOption object, see below | {} |
validationOption.name | Opt | Str | To display in the Error message. i.e Please enter your ${name}. | "" |
validationOption.check | Opt | Bool | To determin if you need to validate. | true |
validationOption.required | Opt | Bool | To determin if it is a required field. | true |
validationOption.showMsg | Opt | Bool | To determin display the error message or not. | true |
validationOption.locale | Opt | Str | For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'. If your are looking for more options, you can take a look at 'window.REACT_INPUTS_VALIDATION' section, which provides the extensibility for your own locale. | "en-US" |
validationOption.msgOnError | Opt | Str | Show your custom error message no matter what when it has error if it is provied. | "" |
validationOption.msgOnSuccess | Opt | Str | Show your custom success message no matter what when it has error if it is provied. | "" |
asyncMsgObj.error | Opt | Bool | (Server response) Backend validation result. | false |
asyncMsgObj.message | Opt | Str | (Server response) Your AJAX message. For instance, provide it when backend returns 'USERNAME ALREADY EXIST' | "" |
asyncMsgObj.showOnError | Opt | Bool | (Server response) Show AJAX error message or not. | true |
asyncMsgObj.showOnSuccess | Opt | Bool | (Server response) Show AJAX success message or not. | false |
Select
Props | Type | Description | Default | |
---|---|---|---|---|
tabIndex | Opt | Str | Num | none | |
id | Opt | Str | "" | |
name | Opt | Str | "" | |
type | Opt | Str | "text" | |
value | Opt | Str | "" | |
disabled | Opt | Bool | false | |
validate | Opt | Bool | false | |
validationCallback | Opt | Func | none | |
optionList | Req | Array | [{id: '1', name: 'Twin Peaks'] | [] |
classNameSelect | Opt | Str | "" | |
classNameWrapper | Opt | Str | "" | |
classNameContainer | Opt | Str | "" | |
classNameOptionListContainer | Opt | Str | "" | |
classNameOptionListItem | Opt | Str | "" | |
customStyleSelect | Opt | Obj | {} | |
customStyleWrapper | Opt | Obj | {} | |
customStyleContainer | Opt | Obj | {} | |
customStyleOptionListContainer | Opt | Obj | {} | |
customStyleOptionListItem | Opt | Obj | {} | |
onBlur | Opt | Func | In order to validate the value on blur, you MUST provide a function, even if it is an empty function. Missing this, the validation on blur will not work. | none |
onChange | Req | Func | (val, e) => {} | |
onFocus | Opt | Func | none | |
onClick | Opt | Func | none | |
selectHtml | Opt | Html | The custom html that will display when user choose. Use it if you think the default html is ugly. | none |
selectOptionListItemHtml | Opt | Html | The custom select options item html that will display in dropdown list. Use it if you think the default html is ugly. | none |
validationOption | Opt | obj | {} | |
validationOption.name | Opt | Str | To display in the Error message. i.e Please enter your ${name}. | "" |
validationOption.check | Opt | Bool | To determin if you need to validate. | true |
validationOption.required | Opt | Bool | To determin if it is a required field. | true |
validationOption.showMsg | Opt | Bool | To determin display the error message or not. | true |
validationOption.locale | Opt | Str | For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'. If your are looking for more options, you can take a look at 'window.REACT_INPUTS_VALIDATION' section, which provides the extensibility for your own locale. | "en-US" |
validationOption.msgOnError | Opt | Str | Show your custom error message no matter what when it has error if it is provied. | "" |
validationOption.msgOnSuccess | Opt | Str | Show your custom success message no matter what when it has error if it is provied. | "" |
asyncMsgObj.error | Opt | Bool | (Server response) Backend validation result. | false |
asyncMsgObj.message | Opt | Str | (Server response) Your AJAX message. For instance, provide it when backend returns 'USERNAME ALREADY EXIST' | "" |
asyncMsgObj.showOnError | Opt | Bool | (Server response) Show AJAX error message or not. | true |
asyncMsgObj.showOnSuccess | Opt | Bool | (Server response) Show AJAX success message or not. | false |
Textarea
Props | Type | Description | Default | |
---|---|---|---|---|
tabIndex | Opt | Str | Num | none | |
id | Opt | Str | "" | |
name | Opt | Str | "" | |
type | Opt | Str | "text" | |
value | Opt | Str | "" | |
disabled | Opt | Bool | false | |
maxLength | Opt | Str | Num | 524288 | |
rows | Opt | Str | Num | 2 | |
cols | Opt | Str | Num | 2 | |
placeholder | Opt | Str | "" | |
validate | Opt | Bool | If you have a submit button and trying to validate all the inputs of your form at once, toggle it to true, then it will validate the field and pass the result via the "validationCallback" you provide. | false |
validationCallback | Opt | Func | Return the validation result. | none |
classNameInput | Opt | Str | "" | |
classNameWrapper | Opt | Str | "" | |
classNameContainer | Opt | Str | "" | |
customStyleInput | Opt | Obj | {} | |
customStyleWrapper | Opt | Obj | {} | |
customStyleContainer | Opt | Obj | {} | |
onChange | Req | Func | (val, e) => {}. Will return the value. | (val, e) => {} |
onBlur | Opt | Func | In order to validate the value on blur, you MUST provide a function, even if it is an empty function. Missing this, the validation on blur will not work. | none |
onFocus | Opt | Func | none | |
onClick | Opt | Func | none | |
onKeyUp | Opt | Func | By providing onKeyUp , the component will perform the checking every time when user types. |
none |
validationOption | Opt | obj | validationOption object, see below | {} |
validationOption.name | Opt | Str | To display in the Error message. i.e Please enter your ${name}. | "" |
validationOption.check | Opt | Bool | To determin if you need to validate. | true |
validationOption.required | Opt | Bool | To determin if it is a required field. | true |
validationOption.type | Opt | Str | Validation type, options are ['string']. | "string" |
validationOption.showMsg | Opt | Bool | To determin display the error message or not. | true |
validationOption.min | Opt | Num | Validation of min length. | 0 |
validationOption.max | Opt | Num | Validation of max length. | 0 |
validationOption.length | Opt | Num | Validation of exact length of the value. | 0 |
validationOption.customFunc | Opt | Func | Custom function. Returns true or err message. | none |
validationOption.reg | Opt | Bool | Custom regex. | "" |
validationOption.regMsg | Opt | Str | Custom regex error message. | "" |
validationOption.locale | Opt | Str | For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'. If your are looking for more options, you can take a look at 'window.REACT_INPUTS_VALIDATION' section, which provides the extensibility for your own locale. | "en-US" |
validationOption.msgOnError | Opt | Str | Show your custom error message no matter what(except the message from customFunc) when it has error if it is provied. | "" |
validationOption.msgOnSuccess | Opt | Str | Show your custom success message no matter what when it has error if it is provied. | "" |
asyncMsgObj.error | Opt | Bool | (Server response) Backend validation result. | false |
asyncMsgObj.message | Opt | Str | (Server response) Your AJAX message. For instance, provide it when backend returns 'USERNAME ALREADY EXIST' | "" |
asyncMsgObj.showOnError | Opt | Bool | (Server response) Show AJAX error message or not. | true |
asyncMsgObj.showOnSuccess | Opt | Bool | (Server response) Show AJAX success message or not. | false |
Phone and email validation example
Custom Error Message (can be multiple locales)
By providing window.REACT_INPUTS_VALIDATION['customErrorMessage']
, you can overwrite the error message.
IMPORTANT NOTE: YOU WILL NEED TO PLACE window.REACT_INPUTS_VALIDATION BEFORE YOUR JS SCRIPT
codesandbox example(located in index.html)
example(fully customized):
Then in the component...
For more extensibility or customization please reffer to 'validationOption.customFunc'