2013-05-02 30 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