第一种方式
1 function check(){
2 var name = document.getElementById("name").value;
3 if(name == null || name == ''){
4 alert("用户名不能为空");
5 return false;
6 }
7 return true;
8 }
9
10 <form name="form" action="跳转的页面" method="post" οnsubmit="return check()">
11 <input type="text" id="name"/>
12 <input type="sumit" value="提交"/>
13 </form>
第二种方式
1 function check(){
2 var name = document.getElementById("name").value;
3 if(name == null || name == ''){
4 alert("用户名不能为空");
5 return false;
6 }
7 return true;
8 }
9
10 <form name="form" action="跳转的页面" method="post">
11 <input type="text" id="name"/>
12 <input type="submit" value="提交" οnclick="return check()"/>
13 </form>