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

查看:208
本文介绍了如何验证HTML文本框不允许特殊字符和空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML:

 < input type =textname =folderName> 

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



如何验证此文本框? 您可以尝试使用此功能:

 < html xmlns =http://www.w3.org/1999/xhtml> 
< head id =Head1runat =server>
< title>< / title>
< script type =text / javascript>
function blockSpecialChar(e){
var k;
document.all? k = e.keyCode:k = e.which; (k> 64& k< 91)||(k>& k< 123)|| k == 8 || k == 32 || (k> = 48& k< = 57));
}
< / script>
< / head>
< body>
< form id =frmrunat =server>
< input type =textname =folderNameonkeypress =return blockSpecialChar(event)/>
< / form>
< / body>
< / 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天全站免登陆