【问题标题】:Radio button validation causing rest of validation to fail单选按钮验证导致其余验证失败
【发布时间】:2013-04-27 04:48:06
【问题描述】:

无线电验证有效,但其余无效。我做错了什么?

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;
    }
}

【问题讨论】:

  • 这么多代码......一个jsFiddle会很好。哦,还有更多信息。到底是什么失败了?
  • 这真的很明显,除非有其他问题,否则不需要小提琴

标签: javascript forms radio-button validation


【解决方案1】:

只需反转测试,您就不必返回

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

   // continue

在无线电测试后你有结束括号,所以脚本的其余部分只是漂浮在网络空间中

最后也只返回一次 true 并且没有多次使用 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;
}

【讨论】:

  • 感谢您的帮助!我在下班前发布了这个,草率证明了我为此工作感到多么恶心!以新鲜的眼光和您的帮助,我已经进行了验证。但是,当我使用您的代码时,只有订单号、电话和地址有效。可能是因为 display 和 form 都声明为 pancettaForm 但 display 是特定于 id 的?我的工作表格每次仍然有 x 声明。我很欣赏合并它的建议,当我有更多时间时,我会继续努力。
  • 为此,您需要在每个字段上都有一个名称。没有看到表格,我无法判断什么可能行不通
猜你喜欢
  • 1970-01-01
  • 2011-05-06
  • 2015-09-01
  • 2012-12-01
  • 2014-04-18
  • 2023-04-06
  • 2015-01-05
  • 2015-03-13
相关资源
最近更新 更多