Create a React JS login page with validation and Bootstrap 5 design

 


import React, { useState } from "react";
import { Form, Button } from "react-bootstrap";
import { useForm } from "react-hook-form";

const LoginPage = () => {
  const { register, handleSubmit, formState: { errors } } = useForm();

  const onSubmit = (data) => {
    // TODO: Send the login credentials to the backend server.
    // If the login is successful, redirect the user to the home page.
    // Otherwise, display an error message.
  };

  return (
    <div className="container">
      <div className="row justify-content-center">
        <div className="col-md-5">
          <Form onSubmit={handleSubmit(onSubmit)}>
            <Form.Group className="mb-3">
              <Form.Label>Email</Form.Label>
              <Form.Control
                type="email"
                name="email"
                placeholder="Enter your email address"
                {...register("email", {
                  required: "Email is required",
                  pattern: {
                    value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
                    message: "Invalid email address",
                  },
                })}
              />
              {errors.email && <span className="text-danger">{errors.email.message}</span>}
            </Form.Group>
            <Form.Group className="mb-3">
              <Form.Label>Password</Form.Label>
              <Form.Control
                type="password"
                name="password"
                placeholder="Enter your password"
                {...register("password", {
                  required: "Password is required",
                  minLength: {
                    value: 8,
                    message: "Password must be at least 8 characters long",
                  },
                })}
              />
              {errors.password && <span className="text-danger">{errors.password.message}</span>}
            </Form.Group>
            <Button type="submit" variant="primary">
              Login
            </Button>
          </Form>
        </div>
      </div>
    </div>
  );
};

export default LoginPage;
Create a React JS login page with validation and Bootstrap 5 design Create a React JS login page with validation and Bootstrap 5 design Reviewed by Bhaumik Patel on 11:57 PM Rating: 5