11

expected UI

I want to re-create this above shown design using an Input field. However, the user should be able to enter only one numeric digit per box. How can I restrict the user to enter only 1 digit per input box. Please suggest.

Edit: this solution solves most of the problem, but does not restrict the input to digits.

Pierre Arnaud
  • 9,862
  • 11
  • 74
  • 105
Sijo Jose
  • 175
  • 1
  • 2
  • 8

4 Answers4

23

Use the maxlength and the pattern to allow only one number

<input type="text" maxlength="1" oninput="this.value=this.value.replace(/[^0-9]/g,'');" />
Pablo Salcedo T.
  • 844
  • 1
  • 17
  • 26
4

You could use the focusout jquery script to check the value isn't greater than 9.

$(document).ready(function() {
  $('input').focusout(function() {
    var max = $(this).val();
    if (max > 9) {
      $(this).val("9");
      alert("Maximum is 9");
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" max="9" Min="0" />
<input type="number" max="9" Min="0" />
<input type="number" max="9" Min="0" />
jbutler483
  • 23,112
  • 9
  • 87
  • 141
3

I would use the number input type, and use max and min. So this should work:

<input type="number" min="0" max="9" step="1">
Mike Harrison
  • 940
  • 1
  • 14
  • 38
0

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
  <form>
   <input type="number" name="abc" min="0" max="9">
  </form>
</body>
</html>
Chirag Gojaria
  • 55
  • 1
  • 10