表单验证忽略了对“名称”输入的验证

时间:2019-02-28 04:54:58

标签: javascript html forms validation

我正在为一个项目制作表格,该表格需要验证。对于电子邮件和机构,验证工作正常,但似乎忽略了名称验证。有人可以帮忙吗?

var name = document.forms["download__form"]["name"],
email = document.forms["download__form"]["email"],
institution = document.forms["download__form"]["institution"];

function validation() {
  if(name.value == "") {
    window.alert("Name must be filled!");
    return false;
  }

  if(email.value == "") {
    window.alert("Name must be filled!");
    return false;
  }

  if(email.value.indexOf("@", 0) < 0) {
    window.alert("Name must be filled!");
    return false;
  }

  if(email.value.indexOf(".", 0) < 0) {
    window.alert("Name must be filled!");
    return false;
  }

  if(institution.value == "") {
    window.alert("Name must be filled!");
    return false;
  }
}
<form class="form__fill" name="download__form" onsubmit="return validation()">
  <input type="text" class="form__input" name="name" placeholder="NAME*" />
  <input type="text" class="form__input" name="email" placeholder="EMAIL*" />
  <input type="text" class="form__input" name="institution" placeholder="INSTITUTION/ORGANIZATION*" />
  <p>*Must be filled</p>
  <input type="submit" name="download" class="form__button" id="form__button" value="Download.PDF" />
</form>

3 个答案:

答案 0 :(得分:0)

工作正常。您已将所有警报消息作为必须填写名称。因此,如果任何字段出现错误,它将发出警报必须填写名称。只需更改与特定字段相关的警报消息,它就不会给名称字段带来不必要的错误

/Users/username/hyperledger-fabric-network
var name = document.forms["download__form"]["name"],
email = document.forms["download__form"]["email"],
institution = document.forms["download__form"]["institution"];

function validation() {
  if(name.value == "") {
    window.alert("Name must be filled!");
    return false;
  }

  if(email.value == "") {
    window.alert("Email must be filled!");
    return false;
  }

  if(email.value.indexOf("@", 0) < 0) {
    window.alert("Email format is not correct!");
    return false;
  }

  if(email.value.indexOf(".", 0) < 0) {
    window.alert("Email format is not correct!");
    return false;
  }

  if(institution.value == "") {
    window.alert("Institution must be filled!");
    return false;
  }
}

答案 1 :(得分:0)

<form class="form__fill" name="download__form" onsubmit="return validation()">
  <input type="text" class="form__input" name="name" placeholder="NAME*" />
  <input type="text" class="form__input" name="email" placeholder="EMAIL*" />
  <input type="text" class="form__input" name="institution" placeholder="INSTITUTION/ORGANIZATION*" />
  <p>*Must be filled</p>
  <input type="submit" name="download" class="form__button" id="form__button" value="Download.PDF" />
</form>

<script type="text/javascript">
// Javascript
var name = document.forms["download__form"]["name"],
email = document.forms["download__form"]["email"],
institution = document.forms["download__form"]["institution"];

function validation() {
  if(name.value == "") {
    window.alert("Name must be filled!");
    return false;
  }

  if(email.value == "") {
    window.alert("email must be filled!");
    return false;
  }

  if(email.value.indexOf("@", 0) < 0) {
    window.alert("email must be valid");
    return false;
  }

  if(email.value.indexOf(".", 0) < 0) {
    window.alert("email must be valid");
    return false;
  }

  if(institution.value == "") {
    window.alert("institution must be filled!");
    return false;
  }
}
</script>

您的代码没有错。。由于所有警报都具有相同的警报值,因此电子邮件验证造成混乱。只需输入正确的电子邮件地址,代码即可正常工作。我也更改了u的值

答案 2 :(得分:0)

这里的问题是您的名称控件没有值,也没有初始化,因此如果键入没有值,它将返回undefined。您需要像这样放支票

if(typeof name.value == "undefined" || name.value =="")

并且您还需要遵循人们在此处给出的建议,以针对不同类型的验证更改消息。

希望这会有所帮助