![](/img/trans.png)
[英]How do I restrict a text input to numbers only but allow numbers from a 10 key?
[英]How can I restrict input to only periods, numbers and semicolons in jQuery?
我想限制用戶可以在輸入文本字段中鍵入哪些字符,以便他們只能鍵入數字,句點和冒號。 因此,如果他們鍵入其他任何內容,它將不會顯示或注冊。 例如,用於IP地址:10.200.66.106:8888
如何在jQuery中實現呢?
我在jsFiddle中寫了一個小例子: http : //jsfiddle.net/6xMxs/
HTML:
<div id="outer">
<div id="inner">
<p>IP Address:</p>
<p class="edit">2323</p>
</div>
<div>
jQuery的:
$(document).ready(function() {
$('.edit').editable('ipaddress/update',{
type : 'textarea',
cancel : 'Cancel',
submit : 'OK',
tooltip : 'Click to edit...'
});
});
您可以使用jquery的字母數字插件來執行此操作。 在您的元素中添加一些類。
$(".myclass").numeric({allow:"."});
您可以刪除不允許的字符:
<input id="ip" type="text" />
$(document).ready(function() {
$('#ip').keyup(function() {
var el = $(this),
val = el.val();
el.val(val.replace(/[^\d\.]/gi, ""));
}).blur(function() {
$(this).keyup();
});
});
但是有更好的方法,例如,您可以完全“阻止”不需要的字符的輸入。 請記住,當不進行服務器端驗證時,您可以輕松避免這些限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.