简体   繁体   中英

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;

在此处输入图像描述

 <form action=""> <input type="text" required pattern="[0-9]{13}" maxlength=13 id="userCode" ng-model="formdata.userCode" title="13 Digit code">&nbsp; <input type="submit"> </form>

You can check here for some other attributes of input to play around with.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM