An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# React Form Validation using [ValidityState object](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState)

![Form Validation Demo](./screenshot.png)

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


}
```