2015-07-20 119 views
2

好吧,我有输入字段:如何在输入字段中只输入10个数字?

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

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

我试着用type="number"但那不是它。任何建议

编辑:所以我可以使用长度为10位数的长度,但是如何限制只有数字? 编辑:模式= “[0-9] *” 不是为我工作

+0

Duplicate - http://stackoverflow.com/questions/113376/character-limit-in-html – 2ne

回答

6

您需要使用maxlength属性

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

什么限制只有数字? – uzhas

+1

如果您想确保输入确切的10个数字,您需要使用pattern'pattern =“[0-9] *”'模式,并且另外使用'minlength =“10”''。 – nikhil

+0

嗯...它不适合我...我仍然可以输入信件 – uzhas

0

为了确保用户仅输入号码,您可以使用pattern属性:

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

这种技术不会在所有的浏览器,看到http://caniuse.com/#feat=input-pattern

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

+0

这将工作,但请注意,并非所有的移动设备都足够智能以触发数字键盘 – Johannes

+0

为真。尝试type =“number”而不是type =“text”。 –

2

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

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

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

<!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>

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/

相关问题