2013-05-02 61 views
0

無線驗證有效,但其餘的驗證無效。我做錯了什麼?單選按鈕驗證導致驗證失敗的其餘部分

function validateRadio(radios) { 
    for (i = 0; i < radios.length; ++i) { 
     if (radios[i].checked) return true; 
    } 
    return false; 
} 

function validateForm() { 
    if (validateRadio(document.forms["pancettaForm"]["updateShip"])) { 
     return true; 
    } else { 
     alert("Please tell us how you would like to update your order."); 
     return false; 
    } 
} 

var x = document.forms["pancettaForm"]["order-number"].value; 
if (x == null || x == "") { 
    alert("Please provide your order number."); 
    return false; 
} 
var x = document.forms["pancettaForm"]["full-name"].value; 
if (x == null || x == "") { 
    alert("Please provide your full name, or the recipients name if you are updating shipping information."); 
    return false; 
} 
var x = document.forms["pancettaForm"]["phone"].value; 
if (x == null || x == "") { 
    alert("Please provide a phone number in case of delivery questions."); 
    return false; 
} 
var display = document.getElementById('address').style.display; 
if (display == 'block') { 
    var x = document.forms["pancettaForm"]["address"].value; 
    if (x == null || x == "") { 
     alert("Please provide your address."); 
     return false; 
    } 
} 
var display = document.getElementById('city').style.display; 
if (display == 'block') { 
    var x = document.forms["pancettaForm"]["city"].value; 
    if (x == null || x == "") { 
     alert("Please provide your city."); 
     return false; 
    } 
} 
var display = document.getElementById('state').style.display; 
if (display == 'block') { 
    if (document.pancettaForm.state.value == "- Select State -") { 
     alert("Please provide your state."); 
     return false; 
    } 
} 
var display = document.getElementById('zip').style.display; 
if (display == 'block') { 
    var x = document.forms["pancettaForm"]["zip"].value; 
    if (x == null || x == "") { 
     alert("Please provide your zip code."); 
     return false; 
    } 
} 
var display = document.getElementById('newShipDate').style.display; 
if (display == 'block') { 
    if (document.pancettaForm.state.value == "- Select Date -") { 
     alert("Please choose your new shipping date."); 
     return false; 
    } 
} 
+0

這麼多代碼...一個jsFiddle會很好。哦,還有更多的信息。像什麼失敗? – Kiruse 2013-05-02 21:12:14

+0

這真的很明顯,除非有其他問題 – mplungjan 2013-05-02 21:25:13

回答

0

只是扭轉測試,因此您不必返回

if(!validateRadio (document.forms["pancettaForm"]["updateShip"])) 
      { 
       alert("Please tell us how you would like to update your order."); 
       return false; 
      } 

    // continue 

你有射頻的測試後的尾架這樣的腳本的其餘部分只是漂浮在網絡空間

最後也只返回一次,並且沒有var x多次,並且在如何訪問表單元素方面一致,並且我還修復了正在測試狀態的日期測試

function validateForm() { 
    var x,display,form = document.forms["pancettaForm"]; 
    if (!validateRadio(form["updateShip"])) { 
    alert("Please tell us how you would like to update your order."); 
    return false; 
    } 

    x = form["order-number"].value; 
    if (x == null || x == "") { 
    alert("Please provide your order number."); 
    return false; 
    } 
    x = form["full-name"].value; 
    if (x == null || x == "") { 
    alert("Please provide your full name, or the recipients name if you are updating shipping information."); 
    return false; 
    } 
    x = form["phone"].value; 
    if (x == null || x == "") { 
    alert("Please provide a phone number in case of delivery questions."); 
    return false; 
    } 
    display = form["address"].style.display; 
    if (display == 'block') { 
    x = form["address"].value; 
    if (x == null || x == "") { 
     alert("Please provide your address."); 
     return false; 
    } 
    } 
    display = form["city"].style.display; 
    if (display == 'block') { 
    x = form["city"].value; 
    if (x == null || x == "") { 
     alert("Please provide your city."); 
     return false; 
    } 
    } 
    display = form['state'].style.display; 
    if (display == 'block') { 
    x = form['state'].value; 
    if (x == "- Select State -") { 
     alert("Please provide your state."); 
     return false; 
    } 
    } 
    display = form['zip'].style.display; 
    if (display == 'block') { 
    x = form["zip"].value; 
    if (x == null || x == "") { 
     alert("Please provide your zip code."); 
     return false; 
    } 
    } 
    display = form["newShipDate"].style.display; 
    if (display == 'block') { 
    x = form["newShipDate"].value; 
    if (x.value == "- Select Date -") { 
     alert("Please choose your new shipping date."); 
     return false; 
    } 
    } 

    return true; 
} 
+0

謝謝你的幫助,沒有小提琴需要!在我離開工作之前,我發佈了這篇文章,而這種滑稽程度證明了我在這方面的工作有多麼不舒服!用新鮮的眼睛和你的幫助我已經驗證工作。但是,當我使用您的代碼時,只有訂單號,電話和地址有效。這可能是因爲顯示和表單都聲明爲pancettaForm,但顯示是特定於ID?我的工作表格每次都會聲明x。我很欣賞將它結合起來的建議,當我有更多時間時,我會努力。 – surfbird0713 2013-05-03 12:48:58

+0

爲了這個工作,你需要在每個領域的名稱。沒有看到表格,我無法判斷什麼可能不起作用 – mplungjan 2013-05-03 13:51:51