Below code says that if all requirements are included in password then after clicking on sign up button page should get redirected to mentioned Href. assist me to correct below code
document.querySelector('form').addEventListener('submit', function(event) {
const password = document.getElementById('password').value;
const requirementsList = document.getElementById('password-requirements').getElementsByTagName('li');
for (let i = 0; i < requirementsList.length; i++) {
requirementsList[i].classList.remove('error');
}
if (password.length < 8) {
requirementsList[0].classList.add('error');
}
if (!/[!@#$%^&*]/.test(password)) {
requirementsList[1].classList.add('error');
}
if (!/[0-9]/.test(password)) {
requirementsList[2].classList.add('error');
}
if (!/[A-Z]/.test(password)) {
requirementsList[3].classList.add('error');
}
if (!/[a-z]/.test(password)) {
requirementsList[4].classList.add('error');
}
let allRequirementsValid = true;
for (let i = 0; i < requirementsList.length; i++) {
if (requirementsList[i].classList.contains('error')) {
allRequirementsValid = false;
}
else {
window.location.href = 'https://chat.openai.com/c/0758cb90-cb9d-4dac-9e16-028c59393619';
}
}
});
i have tried few step. I am expecting that if all requirements are not included in password. then requirements should get highlighted in red after clicking on sign up button. and if requirements are included the page should get redirected to home page after clicking on sign up button.
2
Answers
Used
Array.from()
method to convert the requirements list into an array and then used theforEach()
method to loop through each element. Think of it like going through each item in a shopping list and checking it off.Let me know if it helps and solves the issue, some changes are made but this made it efficient
I will suggest that you use the build in validation for controlling if the form should submit or not. And then for giving the user feedback you can listen for the invalid event. To avoid repeating code use a data-* attribute on each list item and use that for validating the test.
Notice that I use both
e.preventDefault()
and the useCapture parameter onaddEventListener()
for stopping the default message box on the invalid event.