如何验证 html 文本框不允许特殊字符和空格? [英] How to validate html textbox not to allow special characters and space?

查看:23
本文介绍了如何验证 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>

这篇关于如何验证 html 文本框不允许特殊字符和空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆