feat(reset): added a better validator to the form

This commit is contained in:
2021-11-29 01:18:29 +01:00
parent ec451d82c0
commit 442f920681
4 changed files with 106 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
function validate_form() {
var pass = validate_password();
function validate_form(minlength) {
var pass = validate_password(minlength);
return validate_confirm() && pass;
}
@@ -10,28 +10,55 @@ function validate_confirm() {
if (password.value != confirm.value) {
confirm.classList.add("errorinput");
document.getElementById("confirm-msg").classList.add("fade");
document.getElementById("confirm-msg").classList.add("errormsg");
return false;
}
confirm.classList.remove("errorinput");
document.getElementById("confirm-msg").classList.remove("fade");
document.getElementById("confirm-msg").classList.remove("errormsg");
return true;
}
function validate_password() {
function validate_password(minlength) {
// Did the checks pass ?
var status = true;
// Target element
var password = document.getElementById("newpassword");
var reg = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/;
if (reg.test(password.value) != true) {
password.classList.add("errorinput");
document.getElementById("password-msg").classList.add("fade");
return false;
// Check the length
if (password.value.length < minlength)
{
status = false;
document.getElementById("minlen").classList.add("errormsg");
}
else
document.getElementById("minlen").classList.remove("errormsg");
// Look for a digit
if (/.*\d/.test(password.value) != true) {
status = false;
document.getElementById("digit").classList.add("errormsg");
}
else
document.getElementById("digit").classList.remove("errormsg");
// Look for a lowercase char
if (/.*[a-z]/.test(password.value) != true) {
status = false;
document.getElementById("lower").classList.add("errormsg");
}
else
document.getElementById("lower").classList.remove("errormsg");
// Look for an uppercase char
if (/.*[A-Z]/.test(password.value) != true) {
status = false;
document.getElementById("upper").classList.add("errormsg");
}
else
document.getElementById("upper").classList.remove("errormsg");
// Change the color of the inputbox
if (status == false)
password.classList.add("errorinput");
else
password.classList.remove("errorinput");
password.classList.remove("errorinput");
document.getElementById("password-msg").classList.remove("fade");
return true;
return status;
}