React Hook Form Cross-field Validation

React Hook Form Cross-field Validation

A cross-field validation rule is where the rule is dependent on multiple fields. This post covers how to implement these types of validation rules in React hook Form.

A cross-field validation rule is where the rule is dependent on multiple fields. This post covers how to implement these types of validation rules in React hook Form.

React Hook Form Cross-field Validation

An example

We have the following form that captures a low and high score:

type Scores = {
  low: number;
  high: number;
};

const ScoreForm = () => {
  const { register, handleSubmit } = useForm<Scores>();

  const onSubmit = (data: Scores) => {
    console.log("data", data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div className="field">
        <label htmlFor="low">Lowest score</label>
        <input type="number" id="low" name="low" ref={register} />
      </div>
      <div className="field">
        <label htmlFor="high">Highest score</label>
        <input type="number" id="high" name="high" ref={register} />
      </div>
      <button type="submit">Save</button>
    </form>
  );
};

This is a straightforward React Hook Form with no validation rules at the moment.

react native

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

React Native Mobile App Development

Skenix is providing React Native Development Services with qualified React Native App Developers. Get the best React Native App Development Services.

React Native Redux | How To Use Redux In React Native

React Native Redux Example Tutorial From Scratch. We connect the React Native app to Redux store to save and retrieve the application data.

Build a simple React Native Pokemon app with React-Navigation

As we start learning new technologies we want to start building something or work on a simple project to get a better understanding of the technology.