javascript - hide function jquery does not work -


i have code display error when function valid_name not true , hide error when valid_name become true when blur; error display in div hidden. error appears doesn't disappear.

function valid_name() {     if (($("#name").length > 5) && ($("#name").length < 20)) {         return true;     } else {         return false;     } }   $(document).ready(function() {     $('#name').on('blur', function() {         if (!valid_name())             $("#name_erors").text("invalid name").show();         if (valid_name())             $("#name_erors").hide();      }); }); 

i think

$("#name").length 

should

$("#name").val().length 

cause $('#name').length count element found, count character within need use

$("#name").val().length 

so function should be

function valid_name() {     if (($("#name").val().length > 5) && ($("#name").val().length < 20)) {         return true;     } else {         return false;     } } 

you can do

function valid_name() {     var value = $('#name').val(), len = value.length;     return len > 5 && len < 40; } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -