jsp中怎么设置<input type="text" name="usname"/>不能输入空格
正在做一个项目,我刚才在其他网站试过了,他是不能输入空格的,当输入空格的时候会提示出错,那这种效果怎么弄呢?
[解决办法]
<script type="text/javascript">function doit(){ var ev = getEvent(); var k = ev.keyCode || ev.which; var obj = ev.srcElement || ev.target; if(ev.keyCode==32){ alert('不能输入空格'); if(!document.all) obj.value = obj.value.substr(0, obj.value.length-1); return false; } return true;}function getEvent(){ if(document.all) return window.event; func = getEvent.caller; while(func!=null) { var arg0 = func.arguments[0]; if(arg0) { if((arg0.constructor==Event || arg0.constructor ==MouseEvent) ||(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)) return arg0; } func = func.caller; } return null;}</script><input type="text" name="usname" onkeydown="return doit()"/>
[解决办法]
<html><head><script>function nospace(e){if (32 == e.keyCode){ alert("出错"); e.preventDefault();}}</script></head><body><input type="text" name="usname" onkeypress="nospace(event)"/></body></html>