清除空格
<!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>