Javascript validation: Block special characters

后端 未结 7 1937
广开言路
广开言路 2020-12-09 05:50

How can I restrict users from entering special characters in the text box. I want only numbers and alphabets to be entered ( Typed / Pasted ).

Any samples?

相关标签:
7条回答
  • 2020-12-09 06:53

    Try this one, this function allows alphanumeric and spaces:

    function alpha(e) {
        var k;
        document.all ? k = e.keyCode : k = e.which;
        return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
    }
    

    in your html:

    <input type="text" name="name"  onkeypress="return alpha(event)"/>
    
    0 讨论(0)
提交回复
热议问题