I am trying to create an input field with front-end validation. The idea is that when the user types in the password and unfocuses the input field, depending on the criteria that are not met the corresponding span will become red. As soon as they edit the input, if the criteria are met, they will return to the original color. How do I achieve this? I am attaching part of my code.
const inputFields = document.querySelectorAll('.common-input-class');
inputFields.forEach(function(inputField) {
const errorMessage = inputField.parentElement.querySelector('.error-message');
let hasError = false; //variable to track the input error state
inputField.addEventListener('blur', function() {
if (inputField.value.trim() === '') {
inputField.style.border = '1.7px solid red';
errorMessage.style.display = 'block';
hasError = true;
} else {
inputField.style.border = '1.2px solid #ccc';
errorMessage.style.display = 'none';
hasError = false; // Reset the error state when unfocused
}
});
inputField.addEventListener('input', function() {
if (hasError) {
inputField.style.border = '1.7px solid red';
errorMessage.style.display = 'block';
} else {
inputField.style.border = '1.2px solid #ccc'; // Reset the border color on input
errorMessage.style.display = 'none';
}
});
});
const passwordInput = document.getElementById('password');
const passwordErrorMessage = document.querySelector('.nenp .error-message');
const nenpDivs = document.querySelectorAll('.nenp');
let passwordHasError = false; //variable to track the password error state
passwordInput.addEventListener('blur', function() {
if (passwordInput.value.trim() === '') {
passwordInput.closest('.password').style.border = '1.7px solid red';
passwordErrorMessage.style.display = 'block';
} else {
const span1 = nenpDivs[0].querySelector('span:nth-child(1)');
const span2 = nenpDivs[0].querySelector('span:nth-child(2)');
if (span1.style.color === 'red' || span2.style.color === 'red') {
passwordInput.closest('.password').style.border = '1.7px solid red';
passwordHasError = true;
} else {
passwordInput.closest('.password').style.border = '1.2px solid #ccc';
passwordErrorMessage.style.display = 'none';
passwordHasError = false; // Reset the error state when unfocused
}
}
});
passwordInput.addEventListener('input', function() {
const inputValue = passwordInput.value.trim();
nenpDivs.forEach(function(nenpDiv) {
const span1 = nenpDiv.querySelector('span:nth-child(1)');
const span2 = nenpDiv.querySelector('span:nth-child(2)');
if (inputValue.length < 8) {
span1.style.color = 'red'; // Change to red when the criteria are not met
passwordHasError = true;
} else {
span1.style.color = ''; // Reset to the original color when the criteria are met
}
if (/[A-Z]/.test(inputValue) && /d/.test(inputValue)) {
span2.style.color = ''; // Reset to the original color when the criteria are met
passwordHasError = false; // Reset the error state when the criteria are met
} else {
span2.style.color = 'red'; // Change to red when the criteria are not met
passwordHasError = true;
}
});
});
// Add a blur event listener to passwordInput to set passwordHasError when it loses focus
passwordInput.addEventListener('blur', function() {
if (passwordHasError) {
passwordInput.closest('.password').style.border = '1.7px solid red';
} else {
passwordInput.closest('.password').style.border = '1.2px solid #ccc';
}
});
.error-message {
position: relative;
color: rgb(251, 70, 34);
margin-top: -27px;
margin-left: 14px;
display: none;
}
<div class="data-form nenp">
<label for="number">Password </label>
<div class="password">
<input type="password" id="password" autocomplete="off" required>
<div class="visibility">
<a>eye</a>
</div>
</div>
<span class="error-message">required</span>
<div style="margin-top: -10px; display: grid;">
<span>Minimum 6 characters</span>
<span>Must include an Uppercase and number </span>
</div>
</div>
2
Answers
You can get it that way, you just have to adjust it to your criteria
Hello I saw your code and it’s a bit messy to understand at first, but the idea was there, Keep it up!
I got inspired by how it worked and created this, where the code is less repetitive, more readable and easy to change.
Before you see the code be aware that: This code use JS that add classes to items that are being stylized with CSS. To change any of the looks depending on what you want, just change the CSS. Basically JS is linked with CSS.
index.html
Style.css
Code.js
[ Edited ]
How it works:
In js there is a functionality where you can delay the execution of things, you can do it by using the timeout funcion by vanilla JS.
You can use it as this too!
To clear a time out, you have to save it in a variable, otherwise it will give an error
First save the time out in a variable
Secondly when you want to stop it, just type