JS---表單驗證

來源:互聯網
上載者:User

標籤:style   java   color   os   width   io   

(非原創)
<html> 
<title></title> 
  <head> 
  <script language = "javascript"> 
  function cancel(){ 
 
    document.getElementById("Form").reset(); 
  } 
  function check(){ 
  //驗證使用者名稱 
  var username = document.getElementById("username"); 
  if(username.value.length==0){ 
     alert("使用者名稱不可為空!"); 
     username.focus(); 
     return false; 
  } 
  if(username.value.length<6){ 
   alert("使用者名稱長度不能低於6位!"); 
   username.value = ""; 
   username.focus(); 
     return false; 
  } 
   //驗證密碼,確認密碼 
   var pass = document.getElementById("password"); 
   var qrpass = document.getElementById("qrpassword"); 
   if(pass.value.length==0){ 
      alert("密碼不可為空!"); 
      pass.focus(); 
      return false; 
    } 
    if(pass.value.length<6||pass.value.length>15){ 
         alert("密碼的長度為6-15位!"); 
         pass.value = ""; 
         pass.focus(); 
         return false; 
    }else if(pass.value!=qrpass.value){ 
     alert("兩次密碼輸入不一致!"); 
     qrpass.value = ""; 
     qrpass.focus(); 
      return false; 
    } 
    //驗證社會安全號碼 
    var idcard = document.getElementById("idcard"); 
    if(idcard.value.length==0){ 
    alert("請輸入你的社會安全號碼碼!"); 
    idcard.focus(); 
    return false; 
    } 
    var card = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/ 
    if(!card.test(idcard.value)){ 
    alert("你輸入的社會安全號碼碼不正確,請重新輸入!"); 
    idcard.value=""; 
    idcard.focus(); 
    return false; 
    } 
   //驗證性別 
   var sex = document.getElementsByName("sex"); 
   var boo = true; 
   for(var i = 0;i<sex.length;i++){ 
     if(sex[i].checked==true){ 
       boo = false; 
       break; 
     } 
   } 
   if(boo){ 
       alert("請選擇你的性別!"); 
       return false; 
     } 
    //驗證出生日期 
    var data = document.getElementById("birthday"); 
     if(data.value.length==0){ 
      alert("請輸入您的出生日期!"); 
      pass.focus(); 
      return false; 
     } 
    //YYYY-MM-DD || YYYY/MM/DD 的日期格式 
     var a=/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/ 
     if(!a.test(data.value)){ 
       alert("日期格式不正確!\n\n請輸入YYYY-MM-DD或者YYYY/MM/DD的格式!"); 
       return false; 
     }  
    //驗證籍貫 
    var slt=document.getElementById("region"); 
    if(slt.value=="0"){ 
       alert("請選擇你的籍貫!"); 
        return false; 
      } 
    //驗證愛好 
    var flag = true; 
    var aa = document.getElementsByName("hobby"); 
    for(i = 0;i<aa.length;i++){ 
    if(aa[i].checked==true){ 
       flag = false; 
       break; 
      } 
     } 
      if(flag){ 
       alert("請選擇你的愛好!"); 
       return false; 
      } 
    //驗證郵箱   
    var objName = document.getElementById("email"); 
    var pattern = /^[a-zA-Z0-9]{1,}@[a-zA-Z0-9]{1,10}[.](com|cn|net)$/;   
    if(objName.value==0){ 
       alert("請輸入您的郵箱!"); 
       objName.focus(); 
       return false; 
    } 
    if (!pattern.test(objName.value)) {   
           alert("郵箱格式不正確,請重新輸入!"); 
           objName.value=""; 
           objName.focus(); 
           return false;   
          }   
  //驗證手機號碼的 
  var tel = document.getElementById("tel"); 
var pattern= /^1[358]\d{9}$/  
if(tel.value.length==0){ 
   alert("請輸入你的手機號碼!"); 
   tel.focus(); 
   return false; 

if(!pattern.test(tel.value)) {  
   alert("你輸入的手機號碼無效,請重新輸入!"); 
   tel.value=""; 
   tel.focus(); 
     return false;  
}  
  //驗證自我介紹 
  var intro = document.getElementById("intro"); 
  if(intro.value.length==0){ 
   alert("自我介紹不可為空!"); 
    return false; 
   
  }else if(intro.value.length<100){ 
    alert("自我介紹字數不能低於100字!"); 
    return false; 
  } 
  } 
  function checknoAll(){ 
  var all = document.getElementsByName("all"); 
  var no = document.getElementsByName("no"); 
  if(no[0].checked==true){ 
      all[0].checked=false; 
      var all = document.getElementsByName("hobby"); 
     for(i = 0;i<all.length;i++){ 
     if(all[i].checked==true){ 
        all[i].checked=false; 
      }else if(all[i].checked==false){ 
        all[i].checked=true; 
     } 
   } 
 } 
 }  
 function  checkAll(){ 
   var all = document.getElementsByName("all"); 
   var no = document.getElementsByName("no"); 
   if(all[0].checked==true){ 
      no[0].checked=false; 
      var all = document.getElementsByName("hobby"); 
      for(i = 0;i<all.length;i++){ 
      if(!all[i].checked==true){ 
       all[i].checked=true; 
      } 
   } 
 }  

  </script> 
  </head> 
    <body> 
    <form action = "" method = "post" onsubmit = "return check();" id = "Form"> 
       <table border = "1" bordercolor = "green" cellspacing = "1" cellpadding = "1" align = "left" width = "500"> 
       <tr> 
           <td colspan = "2" align = "center"><font size = "5" color = "green"><b>使用者註冊</b></font></td> 
       </tr> 
         <tr> 
             <td align = "right">使用者名稱:</td><td><input type = "text" id = "username" style = "width:150px;height:20px"/></td> 
        </tr> 
        <tr> 
             <td align = "right">密 碼:</td><td><input type = "password" id = "password" style = "width:150px;height:20px"/></td> 
         </tr> 
         <tr> 
             <td align = "right">確認密碼:</td><td><input type = "password" id = "qrpassword" style = "width:150px;height:20px" onpaste="return false"/> <font color = "red">警告:</font>禁止粘貼</td> 
         </tr> 
          <tr> 
             <td align = "right">社會安全號碼:</td><td><input type = "text" id = "idcard" style = "width:150px;height:20px"/></td> 
         </tr> 
         <tr> 
             <td align = "right">性 別:</td><td>男<input type = "radio" id = "sex" name = "sex" value = "boy"/>女<input type = "radio" name = "sex" id = "sex" value = "girl"/></td> 
         </tr> 
         <tr> 
             <td align = "right">出生日期:</td><td><input type = "text" id = "birthday" style = "width:150px;height:20px"/> <font color = "red">YYYY-MM-DD或者YYYY/MM/DD</font></td> 
         </tr> 
         <tr> 
             <td align = "right">籍 貫:</td><td><select id = "region"> 
             <option value = "0">--請選擇籍貫--</option> 
             <option value = "1">河南省鄭州市</option> 
             <option value = "2">河南省信陽市</option> 
             <option value = "3">北京市海澱區</option> 
             <option value = "4">遼寧省大連市</option> 
             <option value = "5">廣東省深圳市</option> 
             </select> 
             </td> 
         </tr> 
         <tr> 
             <td align = "right">愛 好:</td><td><input type = "checkbox" id = "hobby" value = "music"/>音樂 
             <input type = "checkbox" id = "hobby" name = " hobby" value = "basketball"/>籃球 
             <input type = "checkbox" id = "hobby" name = " hobby" value = "visitor"/>旅遊 
             <input type = "checkbox" id = "hobby" name = " hobby" value = "sleep"/>睡覺 
             <font color = "red">*</font> 
              <input type = "checkbox" id = "all" name = " all" onclick = "checkAll()"/>全選 
               <input type = "checkbox" id = "no" name = " bo" onclick = "checknoAll()"/>反選</td> 
         </tr> 
          <tr> 
             <td align = "right">郵 箱:</td><td><input type = "text" id = "email" style = "width:150px;height:20px"/></td> 
         </tr> 
           <tr> 
             <td align = "right">手機號碼:</td><td><input type = "text" id = "tel" style = "width:150px;height:20px"/></td> 
         </tr> 
         <tr> 
             <td align = "right">自我介紹:</td><td><textarea id = "intro" style = "width:150px;height:50px"></textarea></td> 
         </tr> 
         <tr> 
         <td colspan = "2" align = "right"> 
           <input type = "submit" value = " 提交註冊資訊 " />      <input type = "reset" onclick = "cancel()" value = " 全部清空 "/> 
         </td> 
       </table> 
    </form> 
    </body> 
</html> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.