JavaScript表单验证:即使参数为false,if语句也会将select标记运行

时间:2018-05-08 03:07:00

标签: javascript html forms validation

我一直在编写一个脚本来检查表单的字段是否为空。所有这些都运行良好,但对于使用选择和选项标签的生日字段,当选择任何月份时,文本"选择有效日期"仍然归还。我试图只在用户选择"月"时输出该文本。从下拉列表中,如果用户选择任何正常月份,则不会返回。任何帮助将不胜感激!

function validateStudentSignUpForm() {
  var first = document.getElementById("first").value;
  var last = document.getElementById("last").value;
  var email1 = document.getElementById("email1").value;
  var password1 = document.getElementById("password1").value;
  var parentFirst = document.getElementById("parent-first").value;
  var parentLast = document.getElementById("parent-last").value;
  var childFirst = document.getElementById("child-first").value;
  var email2 = document.getElementById("email2").value;
  var password2 = document.getElementById("password2").value;
  var month1 = document.getElementById("option-month1").value;

  // First name can't be blank
  if (first == "") {
    document.getElementById("first").className = document.getElementById("first").className + " error";
    document.getElementById("firstid").innerHTML = "Can't be blank";
  }

  // Last name can't be blank
  if (last == "") {
    document.getElementById("last").className = document.getElementById("last").className + " error";
    document.getElementById("lastid").innerHTML = "Can't be blank";
  }

  // Email can't be blank
  if (email1 == "") {
    document.getElementById("email1").className = document.getElementById("email1").className + " error";
    document.getElementById("email1id").innerHTML = "Can't be blank";
  }

  // Password can't be blank
  if (password1 == "") {
    document.getElementById("password1").className = document.getElementById("password1").className + " error";
    document.getElementById("password1id").innerHTML = "Can't be blank";
  }

  // Parent first can't be blank
  if (parentFirst == "") {
    document.getElementById("parent-first").className = document.getElementById("parent-first").className + " error";
    document.getElementById("parent-firstid").innerHTML = "Can't be blank";
  }

  // Parent last can't be blank
  if (parentLast == "") {
    document.getElementById("parent-last").className = document.getElementById("parent-last").className + " error";
    document.getElementById("parent-lastid").innerHTML = "Can't be blank";
  }

  // Child first can't be blank
  if (childFirst == "") {
    document.getElementById("child-first").className = document.getElementById("child-first").className + " error";
    document.getElementById("child-firstid").innerHTML = "Can't be blank";
  }

  // Email can't be blank
  if (email2 == "") {
    document.getElementById("email2").className = document.getElementById("email2").className + " error";
    document.getElementById("email2id").innerHTML = "Can't be blank";
  }

  // Password can't be blank
  if (password2 == "") {
    document.getElementById("password2").className = document.getElementById("password2").className + " error";
    document.getElementById("password2id").innerHTML = "Can't be blank";
  }

  // Month can't be default
  if (month1 = "na") {
    document.getElementById("option-month1").className = document.getElementById("option-month1").className + " error";
    document.getElementById("date1id").innerHTML = "Choose a valid date";
  }
  return false;
}

1 个答案:

答案 0 :(得分:1)

两个问题:

  1. 您使用的是"npm.exclude": "**/bin/**" 元素的值,而不是optionselect元素的值始终option"na"的值只有select,如果该选项是所选的选项。

  2. 您在比较中使用的是"na"而不是===。由于你只是在那次比较中做到这一点,我认为这是一个错字,而不是误解。

  3. 要解决此问题,请在if (month1 = "na") {上添加id并阅读select,而不是value的{​​{1}},并使用{{1}在比较中。