error message bug solved
This commit is contained in:
parent
e020936de5
commit
5735016d1c
@ -73,20 +73,20 @@ const ChangePassword = () => {
|
|||||||
const [otpError, setOtpError] = React.useState("");
|
const [otpError, setOtpError] = React.useState("");
|
||||||
const [newPasswordError, setNewPasswordError] = React.useState("");
|
const [newPasswordError, setNewPasswordError] = React.useState("");
|
||||||
const [confirmPasswordError, setConfirmPasswordError] = React.useState("");
|
const [confirmPasswordError, setConfirmPasswordError] = React.useState("");
|
||||||
const validatePassword = (password) => {
|
|
||||||
const isStrong =
|
|
||||||
/.{8,}/.test(password) &&
|
|
||||||
/[A-Z]/.test(password) &&
|
|
||||||
/[a-z]/.test(password) &&
|
|
||||||
/[0-9]/.test(password) &&
|
|
||||||
/[!@#$%^&*(),.?":{}|<>]/.test(password);
|
|
||||||
|
|
||||||
if (!isStrong) {
|
|
||||||
return "Password must meet complexity requirements.";
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
|
const validatePassword = (password) => {
|
||||||
|
const rules = [
|
||||||
|
{ regex: /.{8,}/, msg: "At least 8 characters" },
|
||||||
|
{ regex: /[A-Z]/, msg: "At least one uppercase letter" },
|
||||||
|
{ regex: /[a-z]/, msg: "At least one lowercase letter" },
|
||||||
|
{ regex: /[0-9]/, msg: "At least one number" },
|
||||||
|
{ regex: /[!@#$%^&*(),.?\":{}|<>]/, msg: "At least one special character" },
|
||||||
|
];
|
||||||
|
|
||||||
|
const failedRule = rules.find(r => !r.regex.test(password));
|
||||||
|
return failedRule ? `Password must contain: ${failedRule.msg}.` : "";
|
||||||
|
};
|
||||||
|
|
||||||
const handleSubmit = async (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@ -114,15 +114,16 @@ const handleSubmit = async (e) => {
|
|||||||
|
|
||||||
if (hasError) return;
|
if (hasError) return;
|
||||||
|
|
||||||
const passwordError = validatePassword(newPassword);
|
const passwordError = validatePassword(newPassword);
|
||||||
if (passwordError) {
|
if (passwordError) {
|
||||||
setNewPasswordError(passwordError);
|
setNewPasswordError(passwordError);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (newPassword !== confirmPassword) {
|
if (newPassword !== confirmPassword) {
|
||||||
setConfirmPasswordError("Passwords do not match.");
|
setConfirmPasswordError("Passwords do not match.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
@ -211,7 +212,12 @@ const handleSubmit = async (e) => {
|
|||||||
label="New Password"
|
label="New Password"
|
||||||
required
|
required
|
||||||
value={newPassword}
|
value={newPassword}
|
||||||
onChange={(e) => setNewPassword(e.target.value)}
|
onChange={(e) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
setNewPassword(value);
|
||||||
|
const passwordError = validatePassword(value);
|
||||||
|
setNewPasswordError(passwordError);
|
||||||
|
}}
|
||||||
show={showNewPassword}
|
show={showNewPassword}
|
||||||
toggleShow={() => setShowNewPassword((v) => !v)}
|
toggleShow={() => setShowNewPassword((v) => !v)}
|
||||||
placeholder="Enter new password"
|
placeholder="Enter new password"
|
||||||
@ -222,7 +228,11 @@ const handleSubmit = async (e) => {
|
|||||||
label="Confirm New Password"
|
label="Confirm New Password"
|
||||||
required
|
required
|
||||||
value={confirmPassword}
|
value={confirmPassword}
|
||||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
onChange={(e) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
setConfirmPassword(value);
|
||||||
|
if (newPassword === value) setConfirmPasswordError("");
|
||||||
|
}}
|
||||||
show={showConfirmPassword}
|
show={showConfirmPassword}
|
||||||
toggleShow={() => setShowConfirmPassword((v) => !v)}
|
toggleShow={() => setShowConfirmPassword((v) => !v)}
|
||||||
placeholder="Re-enter new password"
|
placeholder="Re-enter new password"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user