2013-03-08 190 views
1

試圖讓我的最終項目準時準備(一次),但我似乎無法在提交表單時驗證單選按鈕組。我有其他的一切工作,並且驗證是如此,然而,廣播組正在固執。我嘗試過不同的方法,例如刪除變量並直接指向'group1',但沒有任何工作。任何幫助將不勝感激。需要幫助驗證單選按鈕

<script type="text/javascript"> 
    function ValidateContactForm() 
    { 
    var name = document.ContactForm.Name; 
    var email = document.ContactForm.Email; 
    var phone = document.ContactForm.areaCode; 
    var what = document.ContactForm.Subject; 
    var comment = document.ContactForm.Comment; 
    var btn = document.ContactForm.group1; 

    if (name.value == "") 
    { 
     window.alert("Please enter your name."); 
     name.focus(); 
     return false; 
    } 

    if (email.value == "") 
    { 
     window.alert("Please enter a valid e-mail address."); 
     email.focus(); 
     return false; 
    } 
    if (email.value.indexOf("@", 0) < 0) 
    { 
     window.alert("Please enter a valid e-mail address."); 
     email.focus(); 
     return false; 
    } 
    if (email.value.indexOf(".", 0) < 0) 
    { 
     window.alert("Please enter a valid e-mail address."); 
     email.focus(); 
     return false; 
    } 

    if (btn.value > 0) { 
     window.alert("Please choose method of contact."); 
     btn.focus(); 
     return false; 
     } 

    if (phone.value == "") 
    { 
     window.alert("Please enter your telephone number."); 
     phone.focus(); 
     return false; 
    } 

    if (what.selectedIndex < 1) 
    { 
     alert("Please tell us how we can help you."); 
     what.focus(); 
     return false; 
    } 

    if (comment.value == "") 
    { 
     window.alert("Please provide a detailed description or comment."); 
     comment.focus(); 
    return false; 
      } 
    } 
    function validateFixedLengthNumericField(numericField, len){ 

    if (len != numericField.value.length){ 
     numericField.focus(); 
     alert('Field must contain exactly '+len+' numbers.'); 
    } 
    } 

    function validateNextField(numericField, len, nextField){ 

    if (len == numericField.value.length){ 
     nextField.focus(); 
    } 
    } 
    </script> 

    <form method="post" onSubmit="return ValidateContactForm();" name="ContactForm"> 
    <div align="center"> 
     <table border="2"> 
    <tr> 
     <td valign="top"><h3 align="left">Contact Information</h3> 
    <p align="left">Name:<br /> <input type="text" size="65" name="Name"></p> 
    <p align="left">E-mail Address:<br /><input type="text" size="65" name="Email"></p> 
    <p align="left">Telephone:<br /> 
     <div class="Telephone" style="float:left;"> 
      (<input name="areaCode" id="areaCode" type="text" maxlength="3" 
       onkeyup="validateNextField(this,3,phonePrefix);" 
       onblur="validateFixedLengthNumericField(this,3);" 
       style="font-size:11px; width:20px;" title="Area Code" autocomplete="off">) 
      <input name="phonePrefix" id="phonePrefix" type="text" 
       onkeyup="validateNextField(this,3,phoneSuffix);" 
       onblur="validateFixedLengthNumericField(this,3);" 
       style="font-size:11px; width:20px;" maxlength="3" title="Phone Prefix" autocomplete="off">- 
      <input name="phoneSuffix" id="phoneSuffix" type="text" maxlength="4" 
       onkeyup="validateNextField(this,3,phoneExtension);" 
       onblur="validateFixedLengthNumericField(this,4);" 
       style="font-size:11px; width:25px;" title="Phone Suffix" autocomplete="off"></p> 
    </div> 
     <p align="left">&nbsp;</p> 
     <p align="left"><strong>Would you like to sign up for one of our mailings?</strong> <br /> 
     </p> 
    <div align="left"> 
     <input type="checkbox" name="option" value="newsletter" /> 
     Newsletters<br /> 
     <input type="checkbox" name="option" value="events" /> 
     Events<br /> 
     <input type="checkbox" name="option" value="grando" /> 
     Grand Openings<br /> 
     <input type="checkbox" name="option" value="coupon" /> 
     Coupons<br /> 
     <input type="checkbox" name="option" value="other" /> 
     Other</div> 
    <p align="left"><strong>How do you perfer us to contact you</strong><br /> 
    </p> 
    <div align="left"> 
     <input type="radio" name="group1" id="r1" value="1" /> 
     Phone 
     <br /> 
    <input type="radio" name="group1" id="r2" value="2" /> 
    Email<br /> 
    <input type="radio" name="group1" id="r3" value="3" /> 
    Snail Mail 
    </div> 
    <p align="left">What can we help you with? 
     <select type="text" value="" name="Subject"> 
      <option> </option> 
      <option>Customer Service</option> 
      <option>Question</option> 
      <option>Comment</option> 
      <option>Complaint</option> 
      <option>Other</option> 
    </select></p> 
    <p align="left">Comments:</p> 
    <p align="center"> 
     <textarea cols="55" rows="10" name="Comment"></textarea> 
    </p> 
    <p align="center"><input type="submit" value="Send" name="submit"><input type="reset"  value="Reset" name="reset">  
     </form> 
+0

得到一些正則表達式去了電子郵件驗證;) – Floremin 2013-03-08 18:06:13

回答

1

在你的情況下,變量btn包含一個單選按鈕數組。所以你需要循環查找哪一個被檢查。喜歡的東西:

var somethingChecked = false; 
for (i = 0; i < btn.length; i++) { 
    if (btn[i].checked) { 
     somethingChecked = true; 
    } 
} 
if (!somethingChecked) { 
    window.alert("Please choose method of contact."); 
    return false; 
} 
+0

工作就像一個魅力...謝謝這麼多。我做的唯一修改是在下面的代碼之後刪除第二個括號:somethingChecked = true; – mikerdz 2013-03-08 18:45:54

+0

我更正了代碼。 「for」後缺少開頭括號。你能把這個標記爲你的問題的答案嗎? – Floremin 2013-03-08 19:02:56

+0

感謝您的幫助。 – mikerdz 2013-03-08 20:23:34