Source: pages-sections/SignUp-Sections/validatePassword.js

/**
 * This function validates the password and reconfirm password fields
 * @param {string} password - The password entered by the user.
 * @param {string} reconfirmPassword - The re-entered password for confirmation.
 * @returns If the password does not meet the specified criteria, the function returns an error message.
 * If the passwords do not match, the function returns an error message.
 * If the password meets the criteria and the passwords match, the function returns null.
 * The function checks the following conditions:
 * - Password must be at least 8 characters long and must contain at least one special character,
 * one number, one uppercase letter, and one lowercase letter.
 * - The re-entered password must match the original password.
 * If the password and reconfirmPassword match and meet the criteria, the function returns null.
 * If the password does not meet the criteria, the function returns an error message.
 * If the passwords do not match, the function returns an error message.
 * If the password and reconfirmPassword match and meet the criteria, the function returns null.
 */

export function validatePassword(password, reconfirmPassword) {
  let specialChars = /[!@#$%^&*(),.?":{}|<>]/;
  let hasSpecial, hasNumber, hasUpperCase, hasLowerCase;
  hasSpecial = hasNumber = hasUpperCase = hasLowerCase = false;

  hasSpecial = specialChars.test(password);
  hasNumber = /\d/.test(password);
  hasUpperCase = /[A-Z]/.test(password);
  hasLowerCase = /[a-z]/.test(password);
  if (
    password.length < 8 ||
    !hasSpecial ||
    !hasNumber ||
    !hasUpperCase ||
    !hasLowerCase
  ) {
    return "Password must be at least 8 characters long and must contain at least one special character, one number, one uppercase letter and one lowercase letter";
  }

  if (password !== reconfirmPassword) {
    return "Passwords do not match";
  }

  return null; // Validation passed
}