How to validate html textbox not to allow special characters and space?(如何验证 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 文本框不允许特殊字符和空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!