Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hook form

npm install react-hook-form //installation
Comment

react hook form

//react hook form with tailwind css
import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => console.log(data);
   
  return (
    <form className="w-full" onSubmit={handleSubmit(onSubmit)}>
                  <div className="form-control w-full mb-5">
                    <label className="">
                      <span className="text-secondary font-semibold text-lg">
                        Email
                      </span>
                    </label>
                    <input
                      type="text"
                      placeholder="example@email.com"
                      className="input input-bordered w-full rounded-full"
                      {...register('email', {
                        required: {
                          value: true,
                          message: 'Email Required !!!',
                        },
                        pattern: {
                          value: /^[^s@]+@[^s@]+.[^s@]+$/i,
                          message: 'Invalid Email Provided !!!',
                        },
                      })}
                    />
                    <label className="level font-bold">
                      {errors.email?.type === 'required' && (
                        <span className="label-text-alt text-red-500">
                          {errors.email.message}
                        </span>
                      )}
                      {errors.email?.type === 'pattern' && (
                        <span className="label-text-alt text-red-500">
                          {errors.email.message}
                        </span>
                      )}
                    </label>
                  </div>
                  <div className="form-control w-full">
                    <label className="">
                      <span className="text-secondary font-semibold text-lg">
                        Password
                      </span>
                    </label>
                    <input
                      type="password"
                      placeholder="abcd123$"
                      className="input input-bordered w-full rounded-full"
                      {...register('password', {
                        required: {
                          value: true,
                          message: 'Password Required !!!',
                        },
                        pattern: {
                          value: /(?=.*[!#$%&?^*@~() "])(?=.{8,})/,
                          message:
                            'Password Must be 8 char including a special char !!!',
                        },
                      })}
                    />
                    <label className="level font-bold">
                      {errors.password?.type === 'required' && (
                        <span className="label-text-alt text-red-500">
                          {errors.password.message}
                        </span>
                      )}
                      {errors.password?.type === 'pattern' && (
                        <span className="label-text-alt text-red-500">
                          {errors.password.message}
                        </span>
                      )}
                    </label>
                  </div>         
          <input type="submit" />
    </form>
  );
}
Comment

React Hook Form

import React from 'react';
import ReactDOM from 'react-dom';
import { connect } from "react-redux";
import useForm from 'react-hook-form';

function SampleForm() {
  const { register, handleSubmit } = useForm();
  const onSubmit = data => {
    alert(JSON.stringify(data));
  };

  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        <div>
          <label htmlFor="firstName">First Name</label>
          <input name="firstName" placeholder="bill" ref={register} />
        </div>

        <div>
          <label htmlFor="lastName">Last Name</label>
          <input name="lastName" placeholder="luo" ref={register} />
        </div>

        <div>
          <label htmlFor="email">Email</label>
          <input name="email" placeholder="bluebill1049@hotmail.com" type="email" ref={register} />
        </div>
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

class Sample extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return <SampleForm />;
  }
}

export default connect()(Sample);

const rootElement = document.getElementById('root');
ReactDOM.render(<Sample />, rootElement);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript arrow functions to create methods inside objects 
Javascript :: how to pass props to another component 
Javascript :: one signal api to send notification 
Javascript :: props in classes 
Javascript :: update head tag metadata vue 
Javascript :: vue-router beforeeach 
Javascript :: joining array of string 
Javascript :: set a variable in express.js 
Javascript :: crypto js 
Javascript :: what is dotenv in nodejs 
Javascript :: inline null check javascript 
Javascript :: angular material moduel 
Javascript :: display unique id number in angular 
Javascript :: angularjs ng-options name value 
Javascript :: what are built in objects in javascript 
Javascript :: how to write a range of numbers in if condition js 
Javascript :: delete node between indexes node list js 
Javascript :: prototype chain in javascript 
Javascript :: function javascript 
Javascript :: how to redirect to another page without writing javascript 
Javascript :: == vs === in javascript 
Javascript :: for check status in ajax javascript 
Javascript :: nan in js 
Javascript :: sort in javascript 
Javascript :: find the sum of an attribute in sequelize 
Javascript :: chatbot using html and javascript 
Javascript :: react-select-search useSelect hook 
Javascript :: discord js check every x minutes 
Javascript :: hide component vuejs 
Javascript :: how to access ::after Pseudo-Elements from javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =