Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

simple reactjs login form

import React, { useState, useEffect } from "react";

const Form = () => {
  const intialValues = { email: "", password: "" };

  const [formValues, setFormValues] = useState(intialValues);
  const [formErrors, setFormErrors] = useState({});
  const [isSubmitting, setIsSubmitting] = useState(false);

  const submit = () => {
    console.log(formValues);
  };

  //input change handler
  const handleChange = (e) => {
    const { name, value } = e.target;
    setFormValues({ ...formValues, [name]: value });
  };

  //form submission handler
  const handleSubmit = (e) => {
    e.preventDefault();
    setFormErrors(validate(formValues));
    setIsSubmitting(true);
  };

  //form validation handler
  const validate = (values) => {
    let errors = {};
    const regex = /^[^s@]+@[^s@]+.[^s@]{2,}$/i;

    if (!values.email) {
      errors.email = "Cannot be blank";
    } else if (!regex.test(values.email)) {
      errors.email = "Invalid email format";
    }

    if (!values.password) {
      errors.password = "Cannot be blank";
    } else if (values.password.length < 4) {
      errors.password = "Password must be more than 4 characters";
    }

    return errors;
  };

  useEffect(() => {
    if (Object.keys(formErrors).length === 0 && isSubmitting) {
      submit();
    }
  }, [formErrors]);

  return (
    <div>
      <h1>Sign in to continue</h1>
      {Object.keys(formErrors).length === 0 && isSubmitting && (
        <span>Form submitted successfully</span>
      )}
      <form onSubmit={handleSubmit} noValidate>
        <div>
          <label htmlFor="email">Email</label>
          <input
            type="email"
            name="email"
            id="email"
            value={formValues.email}
            onChange={handleChange}
          />
          {formErrors.email && <span>{formErrors.email}</span>}
        </div>

        <div>
          <label htmlFor="password">Password</label>
          <input
            type="password"
            name="password"
            id="password"
            value={formValues.password}
            onChange={handleChange}
          />
          {formErrors.password && <span>{formErrors.password}</span>}
        </div>

        <button type="submit">Sign In</button>
      </form>
    </div>
  );
};

export default Form;
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to get href value of anchor tag in jquery in list 
Javascript :: javascript cookie expire in 5 minutes 
Javascript :: get n number of elements from array javascript 
Javascript :: jquery siblings 
Javascript :: Arrays Comparison 
Javascript :: js convert html to text 
Javascript :: how to check the extension of a file in javascript 
Javascript :: Cast to ObjectId failed for value 
Javascript :: reverse 179 in javascript 
Javascript :: remove class element 
Javascript :: js promisify in browser 
Javascript :: postmessage from iframe to parent 
Javascript :: javascript get placeholder value 
Javascript :: javascript delete second last element of array 
Javascript :: delete multiple keys from object javascript 
Javascript :: current date minus days javascript 
Javascript :: On click, disable button 
Javascript :: how to make a deep copy in javascript 
Javascript :: javascript array functions 
Javascript :: lexical scoping javascript 
Javascript :: how to split text into array javascript 
Javascript :: jquery get meta value 
Javascript :: async await useeffect react 
Javascript :: javascript strip 
Javascript :: remove duplicates from array js 
Javascript :: nodejs mysql insert object query 
Javascript :: mysql json array contains 
Javascript :: javascript split multiple delimiters 
Javascript :: express-ejs-layouts install 
Javascript :: hot reload problem react 17 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =