簡體   English   中英

為什么我的 HTML 文件無法使用我鏈接的 JavaScript 文件?

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

盡管已鏈接,但 HTML 文件中並未使用 JavaScript 文件

我無法使用 JavaScript 文件並驗證我的 HTML 表單。 我想知道問題是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
      }


    }

如果您的 JS 文件夾與您的 html 文件位於同一目錄中,則此代碼應該可以工作。 在您的 JS 文件中編寫一個簡單的alert('ahoy')函數並重新加載您的 html 以驗證您的 JS 文件是否已加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM