button 按钮,结合onclick事件,验证和提交表单

<!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>
原文地址:https://www.cnblogs.com/yangzailu/p/6109260.html