This question already has answers here:
How To Only Allow Alpha Numeric Chars With JavaScript

(3个答案)


5年前关闭。




我有类似html的东西,不是完全像这样,只是为了给您一个想法。

<input type="text" disabled="disabled" name="ip" />
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" value="send" />


如何限制仅在名称字段中使用特殊字符?我只想在名称字段中使用字母数字。

你能帮我做到这一点吗?

提前致谢

最佳答案

尝试这个:

var alphanumers = /^[a-zA-Z0-9]+$/;
if(!alphanumers.test($("#firstname").val())){
    alert("Nickname can have only alphabets and numbers.");
}


DEMO

要么

$(document).ready(function () {
    var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    $('.keyup-char').keyup(function () {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();

        if (!charReg.test(inputVal)) {
            $(this).parent().find(".warning").show();
        } else {
            $(this).parent().find(".warning").hide();
        }

    });
});


DEMO2

关于jquery - 如何限制特定形式字段中的特殊字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25448650/

10-11 22:22
查看更多