Eightleven Eightleven - 4 months ago 14
Javascript Question

Best MaxLength and MinLength For Input Field With Integrate With Virtual Keyboard

my problem is in maxlength and minlength notice. if i input manually into input field it's work and notice showing. but if i used virtual keyboard made from jquery the notice don't showing. whether there is a solution and another best code for maxlength and minlength input with virtual keyboard?

Here's my code MaxLength and MinLength Validation:

var minLength = 9;
var maxLength = 12;

$(".minmax").on("keydown keyup change", function(){
var value = $(this).val();
if (value.length < minLength)
$(".noticeminmax").text("Text is short");
else if (value.length > maxLength)
$(".noticeminmax").text("Text is long");
else
$(".noticeminmax").text("");
});

Answer

I don't know what events are triggered by virtual keyboard (if any), but the solution could be to check input value in interval (not optimal solution though)

setInterval(function() {
  var value = $('.minmax').val();
  if (value.length < minLength)
    $(".noticeminmax").text("Text is short");
  else if (value.length > maxLength)
    $(".noticeminmax").text("Text is long");
  else
    $(".noticeminmax").text("");
}, 100 );