gpt4 book ai didi

javascript - 未捕获类型错误 : Cannot read property 'value' of null at signUpValidate (fhe. js :26) at HTMLInputElement. onclick

转载 作者:行者123 更新时间:2023-12-01 01:21:23 24 4
gpt4 key购买 nike

我正在尝试让用户输入他们的信息进行注册。我遇到一个错误,看起来我知道错误是什么,但我不知道如何修复它。请有人帮忙。

未捕获的类型错误:无法在 HTMLInputElement.onclick 的 signUpValidate (fhe.js:26) 处读取 null 属性“值”

HTML 代码:

<!DOCTYPE html>
<html lang="en">`enter code here`
<head>
<script type="text/javascript" src="fhe.js"></script>
<link rel="stylesheet" href="fheCss.css"/>
<meta charset="UTF-8">
<title>Sign Up Page</title>
<h1>Family Home Evening</h1>
<h3>Sign Up</h3>
</head>
<body class="backgroundPic">
<form class="mySignUpForm">
<input type="text" placeholder="First Name" name="firstName" required/><br><br>
<input type="text" placeholder="Last Name" name="lastName" required/><br><br>
<input type="text" placeholder="Username" name="signUpUserName" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{8,20}$" required/><br><br>
<input type="text" placeholder="Password" name="signUpPassword" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" required/><br><br>
<input type="text" placeholder="Verify Password" name="signUpVerPassword" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" required/><br><br>

<input type="button" value="Sign up" id="signup" onclick="signUpValidate()"/><br><br>
<a href="login.html">Back to Login</a><br><br>
</form>

<h4>Username: Username must be between 8 to 20 character<br><br>
Password: Password must contain 1 uppercase, lowercase and number</h4><br><br>
</body>
</html>

JS 代码:

var signUpAttempt = 3;
function signUpValidate() {
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var signUpUserName = document.getElementById("signUpUserName").value;
var signUpPassword = document.getElementById("signUpPassword").value;
var signUpVerPassword = document.getElementById("signUpVerPassword").value;
if (firstName == "Ashish" &&
lastName == "Pokhrel" &&
signUpUserName == "ashishpokhrel" &&
signUpPassword == "ashishpokhrel69" &&
signUpVerPassword == "ashishpokhrel69"){
alert("Signed Up Successful");
window.location = "login.html";
return false;
}
else {
signUpAttempt --;
alert("Please fill out correct input." + "You have " + signUpAttempt + "attempt.");

if(signUpAttempt == 0){
document.getElementById("firstName").disabled = true;
document.getElementById("lastName").disabled = true;
document.getElementById("signUpUserName").disabled = true;
document.getElementById("signUpPassword").disabled = true;
document.getElementById("signUpVerPassword").disabled = true;
document.getElementById("signup").disabled = true;
return true;
}
}
}

最佳答案

您正在使用 document.getElementById() 从表单中获取值,但尚未在输入元素中提供 id 属性。

<input type="text" id="firstName" placeholder="First Name" name="firstName" required/><br><br>
<input type="text" id="lastName" placeholder="Last Name" name="lastName" required/><br><br>
<input type="text" id="signUpUserName" placeholder="Username" name="signUpUserName" pattern="^[a-zA-Z][a-zA-Z0-9-_\.]{8,20}$" required/><br><br>
<input type="text" id="signUpPassword" placeholder="Password" name="signUpPassword" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" required/><br><br>
<input type="text" id="signUpVerPassword" placeholder="Verify Password" name="signUpVerPassword" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" required/><br><br>

关于javascript - 未捕获类型错误 : Cannot read property 'value' of null at signUpValidate (fhe. js :26) at HTMLInputElement. onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54202448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com