https://github.com/smellai/react-form
React Form Validation using ValidityState object
https://github.com/smellai/react-form
form input input-validation react react-hooks reactjs validation validitystate
Last synced: about 1 month ago
JSON representation
React Form Validation using ValidityState object
- Host: GitHub
- URL: https://github.com/smellai/react-form
- Owner: smellai
- Created: 2021-04-15T13:05:06.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-15T13:18:06.000Z (about 5 years ago)
- Last Synced: 2025-04-01T11:48:23.843Z (about 1 year ago)
- Topics: form, input, input-validation, react, react-hooks, reactjs, validation, validitystate
- Language: JavaScript
- Homepage:
- Size: 287 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# React Form Validation using [ValidityState object](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState)

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `yarn start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `yarn build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
## Usage
Declare validation constraints using builtin [input tag](https://www.w3schools.com/tags/tag_input.asp) attributes like:
- required
- pattern
- min
- max
Violations of the declared attributes will be reflected in the [ValidityState object](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState)
```Javascript
import { Input } from "./components/input";
```
For multiple error messages use **errorLabels** instead of single **errorLabel**. errorLabels keys must match the [ValidityState object](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState) ones.
```Javascript
import { Input } from "./components/input";
18",
rangeOverflow: "Please enter a number < 100",
}}
/>
```
To trigger validation on external conditions (e.g. on form submit), set **propagatedTouch** prop to `true`
```Javascript
import React, { useRef, useState } from 'react';
import { Input } from "./components/input";
export const FormComponent = () => {
const [formTouched, setFormTouched] = useState(false);
const formElement = useRef(null);
const handleSubmit = (event) => {
setFormTouched(true);
event.preventDefault();
if (formElement.current.reportValidity()) {
alert('all good!');
}
};
Submit
}
```