2013-04-30 76 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, ''); 
     } 
    }); 
});