2014-03-03 8 views
0

안녕하세요 저는 html과 JS를 처음 사용합니다. 오류가있을 때 다른 정적 사이트로 전송되기 전에 양식을 중지하는 데 문제가 있습니다. 모든 유효성 검사가 작동하지만 오류가 있어도 다른 사이트로 리디렉션됩니다. "action"또는 submit 버튼에 if 문을 코딩해야하는지 잘 모르겠습니다.오류시 유효성 검사 유효성 검사

<!DOCTYPE html> 
<html> 
<head> 
    <title>Week 8 Lab - JavaScript DOM and Arrays</title> 
    <meta charset="utf-8"> 

</head> 
<body> 
<script> 
    function validate() 
    { 
     var fName =document.forms["orderForm"].firstName.value//firstname validation 
     if(fName==null || fName=="") 
     { 
      document.getElementById('firstNameError').innerHTML= "Please enter a first name."; 
     } 


     var lName = document.forms["orderForm"].lastName.value;//last name validation 
     if(lName==null || lName=="") 
     { 
      document.getElementById('lastNameError').innerHTML= "Please enter a last name."; 
     } 


     var address = document.forms["orderForm"].address.value;//address validation 
     if(address==null || address=="") 
     { 
      document.getElementById('addressError').innerHTML= "Please enter an address."; 
     } 


     var city = document.forms["orderForm"].city.value;//city validation 
     if(city==null || city=="") 
     { 
      document.getElementById('cityError').innerHTML= "Please enter a city."; 
     } 


     var pCodeCheck = /^[0-9a-zA-Z]+$/;//postal code validation 
     if(postalCode.value.match(pCodeCheck)) 
     { 
      //do nothing 
     } 
     else 
     { 
      document.getElementById('postalCoderror').innerHTML= "Please enter a valid postal code."; 
     } 

        // makes sure you cannot order a negative number of items 
     var itemQTY = document.forms["orderForm"].widget1qty.value; 
     if(itemQTY < 0) 
     { 
      document.getElementById('qtyError').innerHTML= "You cannot enter a negative number."; 
     } 


     var itemQTY2 = document.forms["orderForm"].widget2qty.value; 
     if(itemQTY2 < 0) 
     { 
      document.getElementById('qtyError2').innerHTML= "You cannot enter a negative number."; 
     } 


     var itemQTY3 = document.forms["orderForm"].widget3qty.value; 
     if(itemQTY3 < 0) 
     { 
      document.getElementById('qtyError3').innerHTML= "You cannot enter a negative number."; 
     } 

        //makes sure there is at least one item ordered 
     var wid1Qty = document.getElementById('widget1qty').value; 
     var wid2Qty = document.getElementById('widget2qty').value; 
     var wid3Qty = document.getElementById('widget3qty').value; 
     if(wid1Qty + wid2Qty + wid3Qty == 0) 
     { 
      document.getElementById('itemQTY').innerHTML= "You must order atleast one item."; 
     } 





    } 
</script> 

    <h2>Order Form</h2> <!-- action="processForm.html"  "javascript:void(0)" --> 
    <form name="orderForm" method="post" onsubmit="validate();" action="processForm.html" > 
    <table> 
     <tr> 
      <th colspan="2">Personal Information</th> 
     </tr> 
     <tr> 
      <td>First Name:</td> 
      <td><input type="text" name="firstName" id="firstName" size="30"></td> 
      <td id="firstNameError"></td> 
     </tr> 
     <tr> 
      <td>Last Name:</td> 
      <td><input type="text" name="lastName" id="lastName" size="30"></td> 
      <td id="lastNameError"></td> 
     </tr> 
     <tr> 
      <td>Address:</td> 
      <td><input type="text" name="address" id="address" size="30"></td> 
      <td id="addressError"></td> 
     </tr> 
     <tr> 
      <td>City:</td> 
      <td><input type="text" name="city" id="city" size="30"></td> 
      <td id="cityError"></td> 
     </tr> 
     <tr> 
      <td>Province:</td> 
      <td><select name="province" id="province" size="1"> 
        <option disabled>Select a province</option> 
        <option value="BC">British Columbia</option> 
        <option value="AB">Alberta</option> 
        <option value="SK">Saskatchewan</option> 
        <option value="MB">Manitoba</option> 
        <option value="ON">Ontario</option> 
        <option value="QC">Québec</option> 
        <option value="NB">New Brunswick</option> 
        <option value="NS">Nova Scotia</option> 
        <option value="PE">Prince Edward Island</option> 
        <option value="NF">Newfoundland</option> 
        <option value="YK">Yukon</option> 
        <option value="NWT">Northwest Territories</option> 
        <option value="NU">Nunavut</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>Postal Code:</td> 
      <td><input type="text" name="postalCode" id="postalCode" maxlength="6"></td> 
      <td id="postalCoderror"></td> 
     </tr> 
     <tr> 
      <th colspan="2">Order Information</th> 
     </tr> 
     <tr> 
      <td rowspan="3">Select your products:<br> 
      <td>Widget #1&nbsp; 
       <input type="text" name="widget1qty" id="widget1qty" size="1" value="0">Qty @ <strong>$5.00/ea</strong></td> 
       <td id="qtyError"></td> 
     </tr> 
     <tr> 
      <td>Widget #2&nbsp; 
       <input type="text" name="widget2qty" id="widget2qty" size="1" value="0">Qty @ <strong>$15.00/ea</strong></td> 
       <td id="qtyError2"></td> 
     </tr> 
     <tr> 
      <td>Widget #3&nbsp; 
       <input type="text" name="widget3qty" id="widget3qty" size="1" value="0">Qty @ <strong>$25.00/ea</strong></td> 
       <td id="qtyError3"></td> 
     </tr> 
     <tr> 
     <td rowspan="3"></td> 
     <td></td> 
     <td id="itemQTY"></td> 
     </tr> 
     <th colspan="3"></th><tr></tr><tr></tr><tr></tr> 
     <tr> 
      <td rowspan="3">Shipping Type:</td> 
      <td>Standard ($5.00)<input type="radio" name="shippingType" id="shippingTypeStandard" value="Standard" checked></td> 

     </tr> 
     <tr> 
      <td>Express ($10.00)<input type="radio" name="shippingType" id="shippingTypeExpress" value="Express"></td> 
     </tr> 
     <tr> 
      <td>Overnight ($20.00)<input type="radio" name="shippingType" id="shippingTypeOvernight" value="Overnight"></td> 
     </tr> 

     <tr> 
      <th colspan="2">Submit Order</th> 
     </tr> 
     <tr> 
      <td><input type="submit" name="btnSubmit" id="btnSubmit" onclick="validate();" value="Submit Order"></td> 
      <td><input type="reset" name="btnReset" id="btnReset" value="Reset Form"></td> 
     </tr> 
    </table> 
    </form> 
</body> 

답변

0

당신이 검증을 중지 오류와 조건에 return false;을 추가 할 경우이 오류가있는 경우가 exemple에 대해 다음과 같이 제출 중지됩니다 :

var fName =document.forms["orderForm"].firstName.value//firstname validation 
if(fName==null || fName=="") 
{ 
    document.getElementById('firstNameError').innerHTML= "Please enter a first name."; 
    return false; 
} 
+0

여러분의 도움에 감사드립니다. – Du6e

0

JavaScript 메소드를 변경하고 유효성을 검사하여 값을 반환하십시오. 필드가 유효한 경우는 true, 그렇지 않은 경우는 false를 리턴합니다. 그러면 양식이 제출되지 않습니다.

+0

감사합니다. – Du6e

관련 문제