主页 > 原创教程 > html+js表单form验证自动提交的2种提交方式button和
html+js表单form验证自动提交的2种提交方式button和
站长设计网页时用到大量的js代码,当然少不了js验证用户输入的合不合格式,格式对就通过验证,自动提交表单,格式不对就弹出错误对话框,重新输入表单。这种判断并提交表单的方法有2种:
1、submit验证
<script>
function checkUser(){
   var result = document.getElementById("userid").value;
   var password = document.getElementById("userpassid").value;
   if(result == ""  ){
     alert("用户名不能为空");
     return false;
   }
   if(password == ""  ){
    alert("密码不能为空");
     return false;
   }else{
   return true;
   }
}
</script>
<form id="formid"  name= "myform" method = 'post'  action = 'user_login_submit.action' onsubmit = "return checkUser();" >
            <table width="100%" border="0">
              <tr>
                <td width="60" height="40" align="right">用户名&nbsp;</td>
                <td><input type="text" value="" class="text2" name = "username" id = "userid"/></td>
              </tr>
              <tr>
                <td width="60" height="40" align="right">密&nbsp;&nbsp;码&nbsp;</td>
                <td><input type="password" value="" class="text2" name = "userpass" id = "userpassid"/></td>
              </tr>
              <tr>
                <td width="60" height="40" align="right">&nbsp;</td>
                <td><div class="c4">
                    <input type="submit" value="" class="btn2"  />
2、button验证
function checkUser(){
   var result = document.getElementById("userid").value;
   var password = document.getElementById("passid").value;

   if(result == ""  ){
     alert("用户名不能为空");
     return false;
   }
   if(password == ""  ){
    alert("密码不能为空");
     return false;
   }
  document.getElementById("formid").submit();
}
form表格的写法,需要写id
 <form id="formid" method = 'post'  action = 'user_login_submit.action'  >

button按钮的写法如下:

<input type="button" value="" class="btn2" onclick = "checkUser();" />

返回顶部
150-9818-8851周一至周六9:00-16:00联系在线客服