禁止表单输入特殊字符

方法一

<html>
<head>
<script>
function regInput(reg){
 var srcElem = event.srcElement
 var oSel = document.selection.createRange()
 var srcRange = srcElem.createTextRange()
 oSel.setEndPoint("StartToStart", srcRange)
 var num = oSel.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(oSel.text.length)
 event.returnValue = reg.test(num)
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<form name="form1" method="post" action="">
  <input name=UserName id="UserName" size=25 maxlength="25" onpaste="return false;" ondragenter="return false" autocomplete="off" onkeypress="regInput(/^[\w]*$/)" value="">
</form>
</body>
</html>

方法 二

 

<input type=text name='name' size=3 onKeyPress='if (event.keyCode < 45 || event.keyCode > 57 ) event.returnValue = false;'>

你可能感兴趣的:(html)