<!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.length<5 || 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>