当前位置: 代码迷 >> Web前端 >> 驱除空格
  详细解决方案

驱除空格

热度:449   发布时间:2012-10-26 10:30:59.0
清除空格
<!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>无标题页</title>
    <script type="text/javascript">
			function checkVal()
			{
				var reg = /^\s+|\s+$/g;
				var oInput = document.getElementById("txt");
				var oValue = oInput.value;
				//清除空格
				oValue = oValue.replace(/^\s+|\s+$/g,"");
				if(reg.test(oValue))
				{
					alert("有空格");
				}
				else
				{
				    alert("没空格");
				}
			}
		</script>
</head>
<body>
<input type="text" value="" id="txt" />
<input type="button" value="提交" onclick="checkVal()" />
</body>
</html>
  相关解决方案