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

the*_*van 7 html javascript validation textbox

这是我的HTML:

 <input type="text" name="folderName">
Run Code Online (Sandbox Code Playgroud)

在这里,我想通过不允许键入特殊字符和空格来验证文本框值.但它应该允许下划线.

如何验证此文本框?

Rah*_*thi 12

您可以尝试使用此功能:

<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>
Run Code Online (Sandbox Code Playgroud)


Dee*_*ran 6

试试这样吧

$(document).ready(function () {
    $("#sub").click(function(){
var fn = $("#folderName").val();
    var regex = /^[0-9a-zA-Z\_]+$/
    alert(regex.test(fn));
});
});
Run Code Online (Sandbox Code Playgroud)

这回归false for special chars and spaces和归来true for underscore, digits and alphabets.

小提琴:http://jsfiddle.net/7C5nP/