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> new document title> meta http-equiv=content-type content=text/html;charset=utf-8 /> style type=text/css>style> script type=text/javascript> //button 按钮,结合onclick事件,验证和提交表单 function checkform(){ //判断用户名是否为空 if(document.form1.username.value==){ window.alert(用户名不能为空); }else if(document.form1.username.value.length5 || document.form1.username.value.length>20){ window.alert(用户名长度必须介于5-50个字符之间!); }else{ //使用form对象的submit()方法,实现提交。 document.form1.submit(); } } script> head> body> form name=form1 method=get action=login.php> 用户名:input type=text name=username /> 密码:input type=password name=userpwd /> input type=button value=提交表单 onclick=checkform() /> form> body>html>