Erico Timbang Erico Timbang - 4 months ago 11
Javascript Question

Jquery If condition on password character count

I'm having a problem with my code. There's no error in Chrome's console but it doesn't work. I wanted to count the password characters entered. The condition is that the password must be a minimum of 6 characters.



$(document).ready(function() {
$(".submit").click(function() {
if($('#regform input[type="password"]').val().length > 6) {
$('.error').html('Must have a min of 6 chars');
$('.error').css("display","block");
}
}
});





Can anyone explain it for me?

Answer

Maybe this is what you're looking for:

HTML

<input type="text" id="password" />
<input type="button" class="btnSubmit" value="Submit" />
<p class="error"></p>

jQuery

$('.btnSubmit').click(function(){
  if($("#password").val().length < 6){
    $('.error').html('Must have a min of 6 chars');
  }else{
    $('.error').html('Good to go.');
  }
});

JSFiddle: https://jsfiddle.net/Ltnrwt0t/