2015-07-20 117 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/

相關問題