jQuery验证除了一些选定的字符

时间:2012-05-02 15:05:36

标签: jquery validation

我需要创建一个验证器。

验证者只需拒绝两个只有" ,的字符并接受其余的字符。

请帮助我任何人

提前感谢:)

这是我现在的代码

<input type="text"/>​

$(document).ready(function () {    
    $('input').keyup(function() {
        var $th = $(this);
        $th.val( $th.val().replace(/[^a-zA-Z0-9]/g, function(str) { alert('You typed " ' + str + ' ".\n\nPlease except.'); return ''; } ) );
    });
});​

DEMO

1 个答案:

答案 0 :(得分:1)

http://jsfiddle.net/ntywf/325/

$(document).ready(function () {

    $('input').keyup(function() {
        var $th = $(this);
        if ((/[\"\,]/g).test($th.val())==true) 
        {

                $th.val( $th.val( ).replace(/[\"\,]/g,function(str) { alert('You typed " ' + str + ' ".\n\nPlease except.');return '';} )) ;
        }
    });
});​