javascript - 为什么我的 HTML 文件无法使用我已链接的 JavaScript 文件?
问题描述
尽管链接了 HTML 文件,但它并未在 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 文件是否已加载。
推荐阅读
- python - 如何获取列表中的每个不同检测对象?
- laravel - 当我让 composer install --optimize-autoloader --no-dev 时找不到类 'BeyondCode\DumpServer\DumpServerServiceProvider'
- java - BorderPane 总是返回空值
- c++ - 如何在 Matlab 中导入 C++ dll
- oracle - 如何在 SQL Developer 中实现多个数据库上多个脚本的自动执行?
- mysql - SQL 按列计数出现次数
- angular - 用 jasmine 测试 NGXS 异常
- node.js - 使用 node.js 监听特定 URL 的请求(不是 LOCALHOST)
- php - MYSQLi 创建数据库?作为名字
- android - 我怎样才能在android中保持“在线”状态