How am I able to check input field’s length?
For example, how to check if name entered in input field has length bigger than 2 characters?
Here is the code that I tried to use, but length always returns 0.
$(document).ready(function () {
name = $("#name").val();
$("#name").change(function () {
if (name.length <= 2) {
console.log(name.length);
$("#name").addClass("error");
} else if (name.length > 2) {
$("#name").removeClass("error");
}
});
});
Just put the
nameassignment inside of thechangeevent.…or here is a different way of writing it…