View Code
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <title></title> 6 </head> 7 <body> 8 <script type="text/javascript"> 9 function JTrim1(s) { 10 return s.replace(/(^\s*)|(\s*$)/g, ""); 11 } 12 13 function JTrim2(s) { 14 return s.replace(/\s+/g, ""); 15 } 16 17 function validator1() { 18 var var1 = document.getElementById("1"); 19 if (JTrim1(var1.value) == "") 20 alert("对不起,你没有输入内容"); 21 else 22 alert("你输入了内容"); 23 } 24 25 function validator2() { 26 var var1 = document.getElementById("1"); 27 if (JTrim2(var1.value) == "") 28 alert("对不起,你没有输入内容"); 29 else 30 alert("你输入了内容"); 31 } 32 </script> 33 <input type= "text" id= "1" value=""/> 34 <br /> 35 <input type="button" name="name" value="vaildator1()" onclick= "validator1();" /> 36 <br /> 37 <input type="button" name="name" value="vaildator2()" onclick= "validator2();" /> 38 <br /> 39 </body> 40 </html>