Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance_Real_Time_Password_Validation_in_Signup_Form #145

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions public/signup.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">

Expand All @@ -19,6 +17,17 @@
.invalid {
border-color: red;
}
.criteria {
list-style: none;
padding-left: 0;
display: none; /* Initially hidden */
}
.criteria li {
color: red;
}
.criteria li.valid {
color: green;
}
</style>
</head>

Expand All @@ -40,11 +49,20 @@ <h2 style="text-align: center; margin: 10px 0px 6px 0px;font-size: 32px;">Regist
<h4 style="text-align: center; margin: 6px 0px 10px 0px;font-size: 22px;">(for Researchers)</h4>
<label for="name">Username:</label>
<input type="text" id="name" name="name" placeholder="Enter the Username" required>

<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter the email" required>
<span id="error-message" class="error">Invalid email domain. Please use Gmail, Outlook, Yahoo, Protonmail, Icloud, or Tutanota.</span>

<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter the Password" required>
<ul id="password-criteria" class="criteria">
<li id="length">At least 8 characters long</li>
<li id="uppercase">At least 1 uppercase letter (A-Z)</li>
<li id="lowercase">At least 1 lowercase letter (a-z)</li>
<li id="number">At least 1 number (0-9)</li>
<li id="special">At least 1 special character (e.g., !@#$%^&*())</li>
</ul>
<button type="submit">Register</button>
</form>
</div>
Expand All @@ -67,15 +85,22 @@ <h4 style="text-align: center; margin: 6px 0px 10px 0px;font-size: 22px;">(for R
document.addEventListener('DOMContentLoaded', function () {
const emailInput = document.getElementById('email');
const errorMessage = document.getElementById('error-message');
const passwordInput = document.getElementById('password');
const passwordCriteria = document.getElementById('password-criteria');

const lengthCriteria = document.getElementById("length");
const uppercaseCriteria = document.getElementById("uppercase");
const lowercaseCriteria = document.getElementById("lowercase");
const numberCriteria = document.getElementById("number");
const specialCriteria = document.getElementById("special");

const allowedDomains = ["gmail.com", "outlook.com", "yahoo.com", "protonmail.com", "icloud.com", "tutanota.com"];

// Validate the email in real-time as the user types
// Show/hide email error message
emailInput.addEventListener('input', function () {
const emailValue = emailInput.value;
const emailDomain = emailValue.split('@')[1];

// Check if email has a valid domain
if (emailDomain && allowedDomains.includes(emailDomain.toLowerCase())) {
emailInput.classList.remove('invalid');
emailInput.classList.add('valid');
Expand All @@ -86,12 +111,68 @@ <h4 style="text-align: center; margin: 6px 0px 10px 0px;font-size: 22px;">(for R
errorMessage.style.display = 'inline';
}

// Remove the error when the user deletes/backspaces
if (emailValue === '') {
emailInput.classList.remove('valid', 'invalid');
errorMessage.style.display = 'none';
}
});

// Show password criteria when the user clicks on the password field
passwordInput.addEventListener('focus', function () {
passwordCriteria.style.display = 'block';
});

// Check if the password input is empty on blur
passwordInput.addEventListener('blur', function () {
if (passwordInput.value === '') {
passwordCriteria.style.display = 'none';
}
});

// Real-time password validation
passwordInput.addEventListener('input', function () {
const password = passwordInput.value;

// Validate length
if (password.length >= 8) {
lengthCriteria.classList.add('valid');
} else {
lengthCriteria.classList.remove('valid');
}

// Validate uppercase letter
if (/[A-Z]/.test(password)) {
uppercaseCriteria.classList.add('valid');
} else {
uppercaseCriteria.classList.remove('valid');
}

// Validate lowercase letter
if (/[a-z]/.test(password)) {
lowercaseCriteria.classList.add('valid');
} else {
lowercaseCriteria.classList.remove('valid');
}

// Validate number
if (/[0-9]/.test(password)) {
numberCriteria.classList.add('valid');
} else {
numberCriteria.classList.remove('valid');
}

// Validate special character
if (/[\W_]/.test(password)) {
specialCriteria.classList.add('valid');
} else {
specialCriteria.classList.remove('valid');
}

// Show the criteria if the password field is not empty
if (password.length > 0) {
passwordCriteria.style.display = 'block';
}
});
});

document.getElementById('registrationForm').addEventListener('submit', async function (event) {
Expand All @@ -105,18 +186,16 @@ <h4 style="text-align: center; margin: 6px 0px 10px 0px;font-size: 22px;">(for R
const result3 = document.getElementById('box3');
const login = document.getElementById('login');

// Allowed email domains
const allowedDomains = ["gmail.com", "outlook.com", "yahoo.com", "protonmail.com", "icloud.com", "tutanota.com"];
const emailDomain = email.split("@").pop();

// Check if the email domain is allowed
if (!allowedDomains.includes(emailDomain)) {
result2.innerHTML = "Invalid email domain. Please use Gmail, Outlook, Yahoo, Protonmail, Icloud, or Tutanota.";
result2.style.display = 'block';
setTimeout(() => {
result2.style.display = 'none';
}, 2000);
return; // Stop form submission
return;
}

try {
Expand Down Expand Up @@ -146,7 +225,6 @@ <h4 style="text-align: center; margin: 6px 0px 10px 0px;font-size: 22px;">(for R
} else if (response.status == 400) {
document.getElementById('password').value = '';
const message = await response.json();
console.log('error', message.error);
result2.innerHTML = message.error;
result2.style.display = 'block';
} else {
Expand Down