React Form Package
Edit page
IntroductionInstallationFormSimple FormButtonFieldFieldWrapperRadioGroupSelectForm ValidationBasic ExampleType validationAdditional rulesStateonFocus onChange onBlurState ManipulationCustom Error MessagesFeedback on disabled ButtonStylingDynamic FieldsDynamic Fields 2Dynamic Fields 3Bind Input FieldsBind Input Fields 2Third Party ComponentsFile UploadWhy

Form Validation

Almost any form needs validation in some way. Fortunatly react-form-package comes with an declarative inbuild validation system.

What does decalrative mean in that way?

Basic Example

If you want that your input field only allows valid emails you can add the type email to the <Field /> component. If you also set validate property on the <Form /> component the button only allowes to submit if the form is completely valid.

In the next example you are able to submit the form if the input is empty (because this field is not required) and if the input value is set to a valid email, but not if the input value is not a valid email.

Email

If you add the property required to the <Field /> component, everything stays the same except that you are not able to submit the form if the input value is empty.

Email

Type validation

A <Field /> component can have diffferent type properties.

Additionally to the validation, if the browser supports HTML5 input types the input fields will be displayed as them, only allowing numbers by default if the type is set to number, etc. If the browser does not support HTML5 it will automatically uses a text input that keeps the validation. If the user inputs a non numeric character in a number input it will display the the error message and you cannot submit the form.

TypeDescription
checkboxno additional validation
datevalidates if the input value matches the format YYYY-MM-DD
textareano additional validation
datetime-localvalidates if the input value matches the format YYYY-MM-DDTHH:MM
emailvalidates if the input value matches the standard email format
numbervalidates if the input value matches a valid number
telvalidates if the input value matches the standard phone number format
textno additional validation
passwordno additional validation
timevalidates if the input value matches the format HH:MM
urlvalidates if the input value matches the standard url format
fileno additional validation

Additional rules

If you are using a <Field /> component you can also add some additional rules for the validation. required will work on all components, the <Field />, the <RadioGroup />, and the <Select />

PropertyTypeDescription
requiredBoolvalidates if the input value is not empty
minString (digit or date (YYYY-MM-DD))text, textarea, password: validates if the input value is at least min characters long; number, date: validates if the input value is at least min
maxString (digit or date (YYYY-MM-DD))text, textarea, password: validates if the input value is maximum max characters long; number, date: validates if the input value is maximum max
matchRegexvalidates if the input value matches the regular expression
sameAsString validates if the input of this field has the same value as the field specified in sameAs
validateFunc a function that gets access to the value of the field (value) => // write your own validation (do not forget to write your own errorMessage)

Example with the required, min, max, validate, and sameAs properties:

Text required, min, max
Number required, min, max
Text is required and has to validate the `validate` function (value === 'react')
Date required, min, max
Password has to be the same as password 2
Password 2 has to be the same as password

Example with the match property:

Text required and has to match "react"

This is just a simple example, off course you are able to pass any regular expression you would like. If you would like to customize the error messages take a look at custom error messages