2013-04-30 73 views
-4

我有一個用於電話號碼的HTML文本框。我想限制用戶使用jQuery將字符輸入到該文本框中。如何限制使用jQuery輸入到文本框中的字符

我試圖用這樣的

<input type="text" onkeyup="this.value = this.value.replace(/[^a-z]/, '')" /> 

但其允許的字符和數字限制

高度讚賞任何想法。

+0

無需對的jQuery的that.html是enought.try如何。 – 2013-04-30 14:07:02

+0

我只是試過這個,但它允許字符和限制數字onkeyup =「this.value = this.value.replace(/ [^ az] /,'')」/> – 2013-04-30 14:08:14

+0

你應該檢查你的PHP代碼中的輸入(或者您在服務器上使用的任何語言),因爲可以在客戶端停用JavaScript。 – Alexxus 2013-04-30 14:13:34

回答

1

您可以使用下面的代碼來驗證號碼的電話號碼:

function checkNumber(that){ 
if(isNaN(that.value)) 
    { 
    alert("Invalid data format.\n\nOnly numbers are allowed."); 
    that.focus(); 
    return (false); 
    } 
    } 

編輯:

<input type="text" onkeyup="checkNumber(this);" /> 

而且你可以看到other codes here

0

試試這個:

$(function(){ 
    $('input[type=text]').on('keyup', function(e){ 
     if (/[^0-9 ]/.test(this.value)) { 
      this.value = this.value.replace(/[^0-9 ]/g, ''); 
     } 
    }); 
});