0

난 그냥 웹 프로그래밍을 배우기 시작 만 5 학기 IT의 학생입니다, 나는 다음과 같은 조건에서 주소 텍스트 영역을를 확인해야 :이 조건문에서 Javascript 형식을 확인 하시겠습니까?

주소 :

  • 해야 채워지 라.
  • 은 14 자 이상이어야합니다.
  • 은 'Street'으로 시작해야합니다.
  • 은 영숫자입니다.
<html> 
    <head> 
    <title>Baggy Bag Shop Membership Registration</title> 
    <link href="mystyle.css" rel="stylesheet" type="text/css"> 
    <script type="text/javascript" lang="javascript"> 
     function validasi() { 
     var nama = document.getElementById("nama"); 
     var password = document.getElementById("password"); 
     var konfirmasi = document.getElementById("konfirmasi"); 
     var email = document.getElementById("email"); 
     var phone = document.getElementById("phone"); 
     var radio1 = document.getElementById("male").checked; 
     var radio2 = document.getElementById("female").checked; 
     var address = document.getElementById("address"); 
     var agreement = document.getElementById("agreement"); 


     if (nama.value == "") { 
      alert("Please fill in your name!"); 
      return false; 
     } 

     if ((nama.value).length < 7) { 
      alert("Username must be more than 6 characters."); 
      return false; 
     } 

     if (password.value == "") { 
      alert("Please fill your password"); 
      return false; 
     } 

     if ((password.value).length < 8) { 
      alert("Password length must be more than 7 characters."); 
      return false; 
     } 

     if (konfirmasi.value == "") { 
      alert("Enter the password confirmation!"); 
      return false; 
     } 

     if (konfirmasi.value != password.value) { 
      alert("Password confirmation does not match!"); 
      return false; 
     } 

     if (email.value == "") { 
      alert("Please enter your E-Mail address!"); 
      return false; 
     } 

     var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; 

     if (reg.test(email.value) == false) { 
      alert('Invalid email address'); 
      return false; 
     } 

     if (phone.value == "") { 
      alert("Please fill out your phone number"); 
      return false; 
     } 

     if (isNaN(phone.value)) { 
      alert("Invalid Phone number!"); 
      return false; 
     } 

     if ((radio1 == "") && (radio2 == "")) { 
      alert("Please state your gender"); 
      return false; 
     } 



     if (agreement.checked != 1) { 
      alert("You must agree to the terms and conditions"); 
      return false; 
     } 

     } 
    </script> 
    </head> 

    <body> 
    <div id="container"> 
     <div id="header">~Baggy Bag Shop~ 
     <div id="logo"> 
      <img src="handbag3.jpg" height="130px" width="300px" /> 
     </div> 
     </div> 
     <div id="menu"> 
     <h2> Menu</h2> 

      <h3><ul><li><a href="home.html">Home</a></li><br> 
        <li><a href="product.html">Products</a></li><br> 
        <li><a href="registration.html">Registration</a></li><br> 
        <li><a href="aboutus.html">About Us</a></li><br> 
        <li><a href="contactus.html">Contact Us</a></li> <ul></h3> 
     </div> 
     <div id="contentarea"> 
     <h1>Create a membership for our newsletter and get special offers!</h1> 

     <form name="membership" action="afterregis.html" method="get" onsubmit="return validasi();"> 
      <table align="center"> 
      <tr> 
       <td>Name:</td> 
       <td> 
       <input type="text" name="nama" id="nama" placeholder="Enter Username" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td> 
       <input type="password" name="password" id="password" placeholder="Enter Password" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Confirm Password:</td> 
       <td> 
       <input type="password" name="konfirmasi" id="konfirmasi" placeholder="Confirm your Password" /> 
       </td> 
      </tr> 
      <tr> 
       <td>E-mail Address:</td> 
       <td> 
       <input type="text" name="email" id="email" placeholder="Enter an Email Address" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Phone:</td> 
       <td> 
       <input type="text" name="phone" id="phone" placeholder="Enter your phone number" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Gender:</td> 
       <td> 
       <input type="radio" name="gender" id="male" value="male" />Male 
       <input type="radio" name="gender" id="female" value="female" />Female</td> 
      </tr> 
      <tr> 
       <td>Address:</td> 
       <td> 
       <textarea cols="30" rows="4"></textarea> 
       </td> 
      </tr> 
      <tr> 
       <td>Terms & Condition:</td> 
       <td> 
       <textarea rows="4" cols="50" readonly>By becoming a member, your data will be used for the sole purpose of Baggy Bag Shop in anyway, including selling it to a third party.</textarea> 
       <br> 
       <input type="checkbox" name="agreement" id="agreement" />I Agreed with the above Terms & Conditions.</td> 
      </tr> 
      <tr> 
       <td align="right"> 
       <input type="submit" id="submit" name="submit" value="SUBMIT" onclick="return validasi" /> 
       </td> 
       <td align="left"> 
       <input type="reset" id="reset" name="reset" value="RESET" /> 
       </td> 
      </tr> 
      </table> 
     </form> 
     </div> 
     <div id="footer"> 
     <p>Copyright&copy;2014</p> 
     </div> 
    </div> 
    </body> 

</html> 
+0

좋아, 그래서 지금 우리는 몇 가지 코드가 있습니다. 지금 정확한 문제는 무엇입니까? 테스트 할 코드가 비어 있습니다. 이제 indexOf ("Street") == 0을 테스트하고 .length> = 1 && .length <= 14 – mplungjan

+0

죄송합니다. 코드를 조건문 섹션에 삽입하는 방법을 모르겠습니다. 정말 새로운, "if"함수가 정확히 일치합니다. – Adrian

+0

ifs 중 하나를 복제하고 전화를 거리로 변경하고 street.value.length를 테스트합니다. – mplungjan

답변

0
  1. 는 이벤트
  2. 당신이 있었다 두 검증 제출하는 대신, 폼 객체가
  3. 가 제출 버튼에 이벤트 핸들러를 추가하지 마십시오 "제출"전화하지 않는 폼에 추가 마십시오 하나는 버튼에, submit 이벤트에는 하나 인

다음은 onload 이벤트에 이벤트를 추가하는 방법입니다. = 텍스트 영역에 "주소"및 성별 버튼에 대한 논리 값에 대한 테스트를 고정 계약 확인란

window.onload = function() { 
 
     document.getElementById("membership").onsubmit = function() { 
 
     var nama = document.getElementById("nama"); 
 
     var password = document.getElementById("password"); 
 
     var konfirmasi = document.getElementById("konfirmasi"); 
 
     var email = document.getElementById("email"); 
 
     var phone = document.getElementById("phone"); 
 
     var radio1 = document.getElementById("male").checked; 
 
     var radio2 = document.getElementById("female").checked; 
 
     var address = document.getElementById("address"); 
 
     var agreement = document.getElementById("agreement"); 
 

 

 
     if (nama.value == "") { 
 
      alert("Please fill in your name!"); 
 
      return false; 
 
     } 
 

 
     if (nama.value.length < 7) { 
 
      alert("Username must be more than 6 characters."); 
 
      return false; 
 
     } 
 

 
     if (password.value == "") { 
 
      alert("Please fill your password"); 
 
      return false; 
 
     } 
 

 
     if (password.value.length < 8) { 
 
      alert("Password length must be more than 7 characters."); 
 
      return false; 
 
     } 
 

 
     if (konfirmasi.value == "") { 
 
      alert("Enter the password confirmation!"); 
 
      return false; 
 
     } 
 

 
     if (konfirmasi.value != password.value) { 
 
      alert("Password confirmation does not match!"); 
 
      return false; 
 
     } 
 

 
     if (email.value == "") { 
 
      alert("Please enter your E-Mail address!"); 
 
      return false; 
 
     } 
 

 
     var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; 
 

 
     if (reg.test(email.value) == false) { 
 
      alert('Invalid email address'); 
 
      return false; 
 
     } 
 

 
     if (phone.value == "") { 
 
      alert("Please fill out your phone number"); 
 
      return false; 
 
     } 
 

 
     if (isNaN(phone.value)) { 
 
      alert("Invalid Phone number!"); 
 
      return false; 
 
     } 
 

 
     if (!radio1 && !radio2) { 
 
      alert("Please state your gender"); 
 
      return false; 
 
     } 
 

 
     if (address.value == "") { 
 
      alert("Please fill your address"); 
 
      return false; 
 
     } 
 

 
     if (address.value.length < 14) { 
 
      alert("Address length must be more than 14 characters."); 
 
      return false; 
 
     } 
 

 
     if (address.value.indexOf("Street")==-1) { 
 
      alert("Address must start with \"Street\"."); 
 
      return false; 
 
     } 
 

 
     if (!agreement.checked) { 
 
      alert("You must agree to the terms and conditions"); 
 
      return false; 
 
     } 
 
     return true; // allow submit 
 
     } 
 
    }
<div id="container"> 
 
    <div id="header">~Baggy Bag Shop~ 
 
    <div id="logo"> 
 
     <img src="handbag3.jpg" height="130px" width="300px" /> 
 
    </div> 
 
    </div> 
 
    <div id="menu"> 
 
    <h2> Menu</h2> 
 
    <h3> 
 
      <ul> 
 
       <li><a href="home.html">Home</a></li><br> 
 
       <li><a href="product.html">Products</a></li><br> 
 
       <li><a href="registration.html">Registration</a></li><br> 
 
       <li><a href="aboutus.html">About Us</a></li><br> 
 
       <li><a href="contactus.html">Contact Us</a></li> 
 
      <ul> 
 
     </h3> 
 
    </div> 
 
    <div id="contentarea"> 
 
    <h1>Create a membership for our newsletter and get special offers!</h1> 
 
    <form id="membership" action="afterregis.html" method="get"> 
 
     <table align="center"> 
 
     <tr> 
 
      <td>Name:</td> 
 
      <td> 
 
      <input type="text" name="nama" id="nama" placeholder="Enter Username" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Password:</td> 
 
      <td> 
 
      <input type="password" name="password" id="password" placeholder="Enter Password" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Confirm Password:</td> 
 
      <td> 
 
      <input type="password" name="konfirmasi" id="konfirmasi" placeholder="Confirm your Password" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>E-mail Address:</td> 
 
      <td> 
 
      <input type="text" name="email" id="email" placeholder="Enter an Email Address" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Phone:</td> 
 
      <td> 
 
      <input type="text" name="phone" id="phone" placeholder="Enter your phone number" /> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Gender:</td> 
 
      <td> 
 
      <input type="radio" name="gender" id="male" value="male" />Male 
 
      <input type="radio" name="gender" id="female" value="female" />Female</td> 
 
     </tr> 
 
     <tr> 
 
      <td>Address:</td> 
 
      <td> 
 
      <textarea id="address" cols="30" rows="4"></textarea> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td>Terms & Condition:</td> 
 
      <td> 
 
      <textarea rows="4" cols="50" readonly>By becoming a member, your data will be used for the sole purpose of Baggy Bag Shop in anyway, including selling it to a third party.</textarea> 
 
      <br> 
 
      <input type="checkbox" name="agreement" id="agreement" />I Agreed with the above Terms & Conditions.</td> 
 
     </tr> 
 
     <tr> 
 
      <td align="right"> 
 
      <input type="submit" value="SUBMIT" /> 
 
      </td> 
 
      <td align="left"> 
 
      <input type="reset" id="reset" name="reset" value="RESET" /> 
 
      </td> 
 
     </tr> 
 
     </table> 
 
    </form> 
 
    </div> 
 
    <div id="footer"> 
 
    <p>Copyright&copy;2014</p> 
 
    </div> 
 
</div>

+0

thx lad, 나는 할 것입니다! – Adrian

+0

또한 조건은 영문자 만 또는 영숫자 여야한다는 것을 알고 있습니까? – Adrian

+0

1) 나는 "아가씨"가 아니다. 2) 알파벳 또는 숫자 자바 스크립트 검색을 위해 SO 또는 Google 검색 - 당신은 자신의 숙제를해야한다. :) – mplungjan

관련 문제