好吧,我有輸入字段:如何在輸入字段中只輸入10個數字?
<input type="text" class="form-control" ng-model="ticketPin">
我想允許用戶只輸入數字,10個digts長(1234567890)
我試着用type="number"
但那不是它。任何建議
編輯:所以我可以使用長度爲10位數的長度,但是如何限制只有數字? 編輯:模式= 「[0-9] *」 不是爲我工作
好吧,我有輸入字段:如何在輸入字段中只輸入10個數字?
<input type="text" class="form-control" ng-model="ticketPin">
我想允許用戶只輸入數字,10個digts長(1234567890)
我試着用type="number"
但那不是它。任何建議
編輯:所以我可以使用長度爲10位數的長度,但是如何限制只有數字? 編輯:模式= 「[0-9] *」 不是爲我工作
爲了確保用戶僅輸入號碼,您可以使用pattern
屬性:
<input type="text" class="form-control" ng-model="ticketPin" pattern="[0-9]{10}">
這種技術不會在所有的瀏覽器,看到http://caniuse.com/#feat=input-pattern
驗證將僅在瀏覽器中執行,不要忘記在服務器上再次驗證數據。
這將工作,但請注意,並非所有的移動設備都足夠智能以觸發數字鍵盤 – Johannes
爲真。嘗試type =「number」而不是type =「text」。 –
你可以這樣做,以確保輸入的值是數字,不超過10位數字。
<input type="text" class="form-control" ng-model="ticketPin" pattern="[0-9]*" maxlength="10">
試試這個插件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>
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這裏:
Duplicate - http://stackoverflow.com/questions/113376/character-limit-in-html – 2ne