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
Post a Comment