如何验证 html 文本框不允许特殊字符和空格?

2022-01-13 00:00:00 validation textbox javascript html

This is my html:

 <input type="text" name="folderName">

Here, I want to validate the textbox value by not allowing to key in special characters and space. But it should allow underscore.

How to validate this textbox?

解决方案

You may try to use this function:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
    function blockSpecialChar(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));
        }
    </script>
</head>
<body>
    <form id="frm" runat="server">
      <input type="text" name="folderName"  onkeypress="return blockSpecialChar(event)"/>
    </form>
</body>
</html>

相关文章