简体   繁体   English

为什么我的 HTML 文件无法使用我链接的 JavaScript 文件?

[英]Why is my HTML file unable to use the JavaScript file that I have linked?

JavaScript file is not used in the HTML file despite linking it尽管已链接,但 HTML 文件中并未使用 JavaScript 文件

I am unable to use the JavaScript file and validate my HTML form.我无法使用 JavaScript 文件并验证我的 HTML 表单。 I am wondering if the issue is the linking of the src directory is wrong or could it be that I am missing something in my JavaScript code.我想知道问题是src目录的链接错误还是我的 JavaScript 代码中缺少某些内容。

文件目录图像

<html>

<head>
  <title>Registration Page</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="js/validation.js" type="text/javascript">
  </script>
</head>

<body>
  <form action="validate" method="post" name="register">
    Full Name: <input type="text" name="name" required/><br/> Email Address: <input type="email" name="email" required/><br/> Address Line 1: <input type="text" name="address1" required/><br/> Address Line 2: <input type="text" name="address2" /><br/>    Postal Code: <input type="number" name="postal" required/><br/> Mobile Number: <input type="number" name="mobile" required/><br/> Password: <input type="password" name="password" required/><br/> Confirm Password: <input type="password" name="cfpassword"
      required/><br/>
    <input type="submit" value="Submit" />
  </form>
</body>

</html>
function validateForm() {

  //Use a regular expression to check for the pattern of the password
  var regexPass = "^[0-9]{6}[a-zA-Z]{1}$";
  var regexMobile = "^[0-9]{8}$";
  var regexPost = "^[0-9]{6}$";
  //Retrieve the VALUE from the "password" field found in the "register" form
  var password1 = document.forms["register"]["password"].value;
  var password2 = document.forms["register"]["cfpassword"].value;
  var postalcode = document.forms["register"]["postal"].value;

  if (matchPost === null) {
    alert("The postal code given in the correct format. Please ensure 
      that is contains exactly 6 digits.
      ");

      // Return false to tell the form NOT to proceed to the servlet
      return false;
    }
    if (matchMobile === null) {
      alert("The mobile number given in the correct format. Please ensure 
        that is contains exactly 8 digits.
        ");

        // Return false to tell the form NOT to proceed to the servlet
        return false;
      }

      // If password not entered 
      if (password1 == '')
        alert("Please enter Password");

      // If confirm password not entered 
      else if (password2 == '')
        alert("Please enter confirm password");

      // If Not same return False.     
      else if (password1 != password2) {
        alert("\nPassword did not match: Please try again...")
        return false;
      }

      // If same return True. 
      else {
        return true
      }


    }

If your JS folder is in the same directory as your html file this code should work.如果您的 JS 文件夹与您的 html 文件位于同一目录中,则此代码应该可以工作。 Write a simple alert('ahoy') function in your JS file and reload your html to verify if your JS file is loaded or not.在您的 JS 文件中编写一个简单的alert('ahoy')函数并重新加载您的 html 以验证您的 JS 文件是否已加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM