我的 Javascript 中的 HTML 表单验证器似乎不起作用

我已经为我的 HTML 编写了一个验证器,尽管我不确定我哪里出错了。


我在下面要做的是确定“名字”框中是否有任何文本。代码中有底层的 css,但我相信我的问题是围绕我的 onsubmit 和 validate 函数,因为一旦我单击提交按钮,javascript 中似乎没有任何东西在运行。


<!doctype html>


<html>

<head>

    <meta charset="utf-8">

  <title>NewPatientForm</title>

  <link rel="stylesheet" type="text/css" href="NewPatient.css">

  

  <script>

    function validate() {

    var invalid = false;



    if(!document.Firstname.value.length) {

        invalid = true;

    }

  

    if(invalid) {

        document.getElementById("form-error").style.display = "inline-block";

        return false; //to make the text appear

    }

    return true;

}


  </script>

</head>

<body>

  <form id="NewPatientForm" method="post" action="#" onsubmit="return  validate();">

  <div class="form-element">

  <p id="form-error">All fields are required</p>

  </div>

  

  <div>

  <label for="Firstname">First Name

  <input type="text" name="Firstname" placeholder="First Name" id="Firstname">

  </label>

  </div>


  <div>

  <input type="submit" name="submit-button" value="Submit">

  </div>

  </form>

</body>

</html>


www说
浏览 214回答 2
2回答

杨魅力

看起来罪魁祸首是您试图访问Firstname该document对象。我用更标准的document.getElementById()方法和它的工作替换了它。一些阅读:Do DOM tree elements with ids become global variables?function validate() {&nbsp; var invalid = false;&nbsp; if(!document.getElementById('Firstname').value.length) {&nbsp; &nbsp; &nbsp; invalid = true;&nbsp; }&nbsp; if(invalid) {&nbsp; &nbsp; &nbsp; document.getElementById("form-error").style.display = "inline-block";&nbsp; &nbsp; &nbsp; return false;&nbsp; }&nbsp; return true;}#form-error {&nbsp; display: none;}<form id="NewPatientForm" method="post" action="#" onsubmit="return validate()">&nbsp; <div class="form-element">&nbsp; &nbsp; <p id="form-error">All fields are required</p>&nbsp; </div>&nbsp;&nbsp;&nbsp; <div>&nbsp; &nbsp; <label for="Firstname">First Name&nbsp; &nbsp; <input type="text" name="Firstname" placeholder="First Name" id="Firstname">&nbsp; &nbsp; </label>&nbsp; </div>&nbsp; <div>&nbsp; &nbsp; <input type="submit" name="submit-button" value="Submit">&nbsp; </div></form>

墨色风雨

有几个错别字,我也会提出其他建议。首先,代码中的一个或三个修复:<!doctype html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <title>NewPatientForm</title>&nbsp; <script>&nbsp; &nbsp; function validate() {&nbsp; &nbsp; &nbsp; const invalid = document.getElementById("Firstname").value.length == 0;&nbsp; &nbsp; &nbsp; if(invalid) {&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("form-error").style.display = "inline-block";&nbsp; &nbsp; &nbsp; &nbsp; return false; //to make the text appear&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; </script></head><body><form id="NewPatientForm" method="post" action="#" onsubmit="return validate();">&nbsp; &nbsp; <div class="form-element">&nbsp; &nbsp; &nbsp; &nbsp; <p id="form-error">All fields are required</p>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <label for="Firstname">First Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="Firstname" placeholder="First Name" id="Firstname">&nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="submit-button" value="Submit">&nbsp; &nbsp; </div></form></body></html>我的建议是您还可以查看内置的 HTML 表单验证属性。我认为您正在重新发明轮子,例如要求非空名字。为什么不用这个而不是 JavaScript?<input type="text" name="Firstname" id="Firstname" placeholder="First Name" required />还有很多其他的,比如 minlength=""、min=""、step="" 等。此外,还有一个带有 .checkValidity() 的验证系统中的 JavaScript 挂钩,因此您可以让内置验证完成繁重的工作,然后也加入更多您自己的自定义方面。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript