-3

I want to limit input field to one letter only(with JS),after one I want to get an error. How to do it?

Sreeragh A R
  • 2,871
  • 3
  • 27
  • 54

1 Answers1

1

You will want something like this

Vanilla JS

<input onkeydown="checkLen(this)" type="text" maxlength="1" />

function checkLen(e) {
  if (el.value.length > 1) {
    alert("ERROR");
  }
}

jQuery

$('#input').keydown( function(e){
    if ($(this).val().length > 1) { 
        alert('ERROR');
    }
});
DNKROZ
  • 2,634
  • 4
  • 25
  • 43