如何在输入字段中只输入10个数字?

时间:2015-07-20 13:56:10

标签: angularjs html5

好的我有输入字段:

<input type="text" class="form-control" ng-model="ticketPin">

我想允许用户只输入10个数字长度的数字(1234567890)

我尝试了type="number",但事实并非如此。任何建议

编辑:所以我可以使用maxlength长10位数,但是仅限于数字呢? 编辑:模式=&#34; [0-9] *&#34;对我不起作用

5 个答案:

答案 0 :(得分:6)

您需要使用maxlength属性

<input type="text" class="form-control" ng-model="ticketPin" maxlength="10">

答案 1 :(得分:2)

您可以这样做以确保输入的值是数字且不超过10位。

<input type="text" class="form-control" ng-model="ticketPin" pattern="[0-9]*" maxlength="10">

答案 2 :(得分:2)

试试这个插件http://candreoliveira.github.io/bower_components/angular-mask/examples/index.html#/

&#13;
&#13;
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
  <script src="//rawgit.com/candreoliveira/ngMask/master/dist/ngMask.min.js"></script>

  
</head>
<body ng-app="selectExample">
  <script>
    angular.module('selectExample', ['ngMask'])
  </script>
  <div>
     <input type='text' mask-clean='true' ng-model='ticketPin' mask='9999999999' restrict="reject" clean="true" />
  </div>
   {{ticketPin}}
  </body>

</html>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

要确保用户仅输入数字,您可以使用pattern属性:

<input type="text" class="form-control" ng-model="ticketPin" pattern="[0-9]{10}">

此技术不适用于所有浏览器,请参阅http://caniuse.com/#feat=input-pattern

验证只能在浏览器中执行,不要忘记在服务器上再次验证数据。

答案 4 :(得分:0)

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"] {
    -moz-appearance: textfield;

}
 <input id="Phone" onkeypress="return isNumeric(event)" oninput="maxLengthCheck(this)" type="number" max = "9999999999" placeholder="Phone Number" />

<script>
  function maxLengthCheck(object) {
    if (object.value.length > object.max.length)
      object.value = object.value.slice(0, object.max.length)
  }
    
  function isNumeric (evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode (key);
    var regex = /[0-9]|\./;
    if ( !regex.test(key) ) {
      theEvent.returnValue = false;
      if(theEvent.preventDefault) theEvent.preventDefault();
    }
  }
</script>

jsfiddle在这里:

https://jsfiddle.net/DharaPatel0621/mo9qgk31/

相关问题