js判斷所有表單項不為空白則提交表單

來源:互聯網
上載者:User

標籤:js判斷所有表單項不為空白則提交表單

demo1.html

<html>    <head>        <title>提交表單頁</title>        <script type="text/javascript">            function myCheck()            {               for(var i=0;i<document.form1.elements.length-1;i++)               {                  if(document.form1.elements[i].value=="")                  {                     alert("當前表單不能有空項");                     document.form1.elements[i].focus();                     return false;                  }               }               return true;                          }        </script>    </head>    <body>         <form name="form1" method="post" action="page2.html" onSubmit="return myCheck()">             使用者名稱:<input type="text" name="username"><br>             性別:<input type="text" name="sex"><br>             出生時間:<input type="text" name="birthday"><br>             <input type="submit" value="提交">         </form>    </body></html>

demo2.html

<script language="Javascript"><!--function submit1(frm){   if (document.frm.word.value=="") {      alert("請輸入搜尋新聞內容!");document.frm.word.focus();return false;}return true;} //--></script><form method="POST" action="" name="frm" onsubmit="return submit1(this);"><select name="select0"> <option value="0">請選擇一個類別</option><option value="1">新聞</option><option value="2">產品</option><option value="3">留言</option></select><input type="text" name="word" size="20" class="input" style="height:19px; "><input type="radio" name="sea_cat" value="標題">標題  <input type="radio" name="sea_cat" value="內容">內容<input type="submit" value="搜" name="B1" class="input1" >

demo3.html

<script type="text/javascript">function beforeSubmit(form){if(form.username.value==''){alert('使用者名稱不可為空!');form.username.focus();return false;}if(form.password.value==''){alert('密碼不可為空!');form.password.focus();return false;}if(form.password.value.length<6){alert('密碼至少為6位,請重新輸入!');form.password.focus();return false;}if(form.password.value!=form.password2.value) {alert('你兩次輸入的密碼不一致,請重新輸入!');form.password2.focus();return false;}return true;}</script><fieldset>   <legend>使用者註冊</legend>    <form method="post" name="form" action="user.do?method=register" onSubmit="return beforeSubmit(this);">     <table border="1" width="100%" cellspacing="0" cellpadding="0">      <tr><td><label>使用者名稱:<input type="text" name="username" value=""></label></td></tr>      <tr><td><label>密   碼:<input type="password" name="password" value=""></label></td></tr>      <tr><td><label>重複密碼:<input type="password" name="password2" value=""></label></td></tr>      <tr><td><input value="註冊" type="submit"> <input type="reset" value="重設"></td></tr>           </table>    </form></fieldset>



js判斷所有表單項不為空白則提交表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.