password confirmation not allow 3 digit password

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


password confirmation not allow 3 digit password



I created a simple password confirmation
that will auto check password if match without
clicking the submit button if not match the button is
disable and if passsword match the button if enable, Now my problem
is, Is there a way that the code will check if the
password is at least 5 digits on it and if not it will
show a message that he/she must enter atleast 5 digits
password? hope you can help me.



this is my current script for checking password:


<script>
$('#password, #password1').on('keyup', function () {
if (!$('#password').val()) {
$('#message').html('');
} else {
if ($('#password').val() == $('#password1').val()) {
$('#message').html('Password Match').css('color', 'green');
$('#submit').prop('disabled', false);
} else {
$('#message').html('Password not Match').css('color', 'red');
$('#submit').prop('disabled', true);
}
}
});
</script>





You mean you want to see if the password contains at least 5 digits or it at least more than 5 characters?
– NewToJS
28 mins ago





yes, or it will check if the is atleast 5 digits on it
– Ban
15 mins ago




2 Answers
2



Try adding the following validation to your code


if ($('#password').val().length < 5) {
$('#message').html('Password must be at least 5 digits').css('color', 'red');
$('#submit').prop('disabled', true);
}





i tried puting the code on top of if but for some reason its not working, im sorry im a beginner at javascript and jquery.
– Ban
18 mins ago





If you want to make the validation on the keyup function you got there, try adding it in before this line if ($('#password').val() == $('#password1').val()) {
– Pietro Nadalini
15 mins ago




if ($('#password').val() == $('#password1').val()) {





Thank you Pietro Nadalini now its working:)
– Ban
6 mins ago







No problem Ban, you can mark my question as accepted if my solution was correct
– Pietro Nadalini
4 mins ago



I don't have enough rep to comment on Nadalini's answer and the code is just missing one closing bracket for the if condition. I can't edit too because its just one character to be added.


if



By the way for a little of off topic discussion, I would like to recommend you to considered using change event in this condition too because it could prevent the user to autofill the form without firing the keyup function. :)


change


keyup



ref : Differences between keyup() event and change() event in username live change



In this case the code can be:


<script>
$('#password, #password1').on('keyup change', function () {
if (!$('#password').val()) {
$('#message').html('');
}else if($('#password').val()<5){
$('#message').html('Password must be at least 5 digit!').css('color', 'red');
$('#submit').prop('disabled', false);
} else {
if ($('#password').val() == $('#password1').val()) {
$('#message').html('Password Match').css('color', 'green');
$('#submit').prop('disabled', false);
} else {
$('#message').html('Password not Match').css('color', 'red');
$('#submit').prop('disabled', true);
}
}
});
</script>



So throughout the validation steps, we only start to match the password if the password is at least 5 digit





Thank you gitguddoge for the ref. :)
– Ban
4 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Will Oldham

Makefile test if variable is not empty