Sometimes, we want to validate that a password is at least 6 character long with JavaScript.
In this article, we’ll look at how to validate that a password is at least 6 character long with JavaScript.
How to validate that a password is at least 6 character long with JavaScript?
To validate that a password is at least 6 character long with JavaScript, we can check if the length
property of the input value string is at least 6.
For instance, we write:
<input type='password' />
to add a password input.
Then we write:
const input = document.querySelector('input')
input.onchange = (e) => {
if (e.target.value.length >= 6) {
console.log('valid')
}
}
We select the input with querySelector
.
Then we set the input.onchange
property to a function that checks if e.target.value.length
is bigger than or equal to 6.
e.target.value
is the input value.
If e.target.value.length
is bigger than or equal to 6, then the password is valid.
Conclusion
To validate that a password is at least 6 character long with JavaScript, we can check if the length
property of the input value string is at least 6.