I want to limit input field to one letter only(with JS),after one I want to get an error. How to do it?
Asked
Active
Viewed 459 times
-3
-
1use the oninput event. limit the length to 1 – Semi-Friends Jun 20 '18 at 08:11
-
show the code and error please – TAHA SULTAN TEMURI Jun 20 '18 at 08:12
-
1why not use a pattern like: "^.{1}$" in the input field? instead of check the error in js – Margon Jun 20 '18 at 08:20
1 Answers
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