Search code examples
htmlinputmaxlength

Set the exact number of elements required for an Input field with type number


I have a field input with type number in my form I would like to allow exact 13 characters to that field no less no more as per the requirement. I wanted to know is there any by default option in HTML using which I can accomplish this? I tried pattern and maxlength but for some reason they are ineffective with the type number. If it is not possible from HTML side then I would like to achieve the same using the angularjs. Any inputs would be really useful.

<input type="number" id="userCode" ng-model="formdata.userCode" title="13 Digit code"></input>&nbsp;

As per the below answer I added the min and max as 13. After adding it when I enter even 13 digits then also I get the error. Can someone please let me know what am I doing wrong here?

<input type="number" min="13" max="13" id="userCode" ng-model="formdata.userCode" placeholder="Enter 13 Digit Code" title="Please Enter the 13 digit code" class="form-control" style="width: 200px;" ng-required="true">&nbsp;

enter image description here


Solution

  • If anyone is looking they can also check this answer:

    <input type="text" pattern=".{13,13}" oninput="this.value=this.value.replace(/[^0-9]/g,''); title="13 characters length required" ">
    

    With this input field although its text we can make sure that it accepts only the numbers.