日期:2014-05-16  浏览次数:20336 次

求空字符串验证方法
各位大虾请帮忙:
   两个问题:
     1.在文本框中禁止输入空格
     2.将文本框中空格清除

------解决方案--------------------
<input name= "aaa " onblur= "if(/[ ]/.test(this.value)){alert( '错误 ');this.value= ' '} ">
<input name= "aaa " onblur= "this.value=this.value.replace(/[ ]/g, ' ') ">
------解决方案--------------------
L@_@K

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<title> clear space </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<meta name= "keywords " content= "javascript " />
<meta name= "description " content= "for javascript region of csdn " />
</head>

<body>
<input type= "text " id= "txtInput " />
<input type= "button " id= "btnCheck " value= "Check " />
</body>
<script type= "text/javascript ">
<!--
String.prototype.clearSpace = function()
{
return this.replace(/\s*/g, " ");
}

var oText = document.getElementById( "txtInput ");
var oCheck = document.getElementById( "btnCheck ");

oText.onkeydown = function() {
if (event.keyCode==32)
{
event.returnValue = false;
}
};

oCheck.onclick = function() {
oText.value = oText.value.clearSpace()
};

//-->
</script>
</html>

------解决方案--------------------
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function a(obj){
if(event.keyCode==32){
obj.value=obj.value.replace(/[\s ]/g, " ");
}
}
</script>
</head>
<body>
<input type=text value= " " onkeyup= "a(this) ">
</body>
</html>