【发布时间】:2022-01-07 21:31:43
【问题描述】:
我正在使用 HTML 代码中包含的一些 CSS 以及 HTML 中还包含 javascript 验证来详细说明简单的 HTML 表单。我无法识别任何错误,但表单中的输入数据由于某种原因无法验证...任何想法可能是代码问题?我尝试了不同的浏览器(Chrome、Edge),但还没有成功。 非常感谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
<script>
function checkField1() {
var field = document.getElementById("Name").value;
var regex = /^[a-zA-Z\s]*$/; //Only letters allowed
if (regex.test(field))
document.getElementById("message1").innerHTML = "Input accepted!";
else document.getElementById("message1").innerHTML = "Please enter your name!";
}
function checkField2() {
var field = document.getElementById("Address").value;
var regex = /^[0-9a-zA-Z\s]*$/; //Only alphanumerical characters allowed
if (regex.test(field))
document.getElementById("message2").innerHTML = "Input accepted!";
else document.getElementById("message2").innerHTML = "Please enter your address!";
}
function checkField3() {
var field = document.getElementById("Email").value;
var regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; //Only correct email format allowed
if (regex.test(field))
document.getElementById("message3").innerHTML = "Input accepted!";
else document.getElementById("message3").innerHTML = "Please enter valid email address!";
}
function checkField4() {
var field = document.getElementById("Password").value;
var regex = /^[0-9a-zA-Z]{8}$/; //Only alphanumerical 8 character password allowed
if (regex.test(field))
document.getElementById("message4").innerHTML = "Input accepted!";
else document.getElementById("message4").innerHTML = "Please enter 8 character password!";
}
function checkField5() {
var field = document.getElementById("RepeatPassword").value;
var regex = /^[0-9a-zA-Z]{8}$/; //Only alphanumerical 8 character password allowed
if (regex.test(field))
document.getElementById("message5").innerHTML = "Input accepted!";
else document.getElementById("message5").innerHTML = "Please enter 8 character password!";
}
function checkField6() {
var field = document.getElementById("Telephone").value;
var regex = /^[0-9]{10}$/; //Only 10 character phone number allowed
if (regex.test(field))
document.getElementById("message6").innerHTML = "Input accepted!";
else document.getElementById("message6").innerHTML = "Please enter your mobile phone!";
}
function checkPassword() {
var pw1 = document.getElementById("Password");
var pw2 = document.getElementById("RepeatPassword"); //Password confirmation check
if (pw1 != pw2)
document.getElementById("messagePWD").innerHTML = "Passwords did not match";
else document.getElementById("messagePWD").innerHTML = "Password created successfully";
}
</script>
<style>
/* This are inline styles applicable to registration form */
body {
background-color: antiquewhite;
}
div {
box-sizing: border-box;
width: 100%;
border: 100px solid black;
float: left;
align-content: center;
align-items: center;
}
form {
margin: 0 auto;
width: 600px;
}
</style>
</head>
<body>
<! If you click the "Submit" button, the form-data will be sent to a page called "/submit.php" >
<h1 style="text-align: center;">REGISTRATION FORM</h1>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Harvard_University_logo.svg/2560px-Harvard_University_logo.svg.png" alt="Harward University Logo" width="500" height="100" margin:auto>
<p style="text-align: center; ">Do you want to register for a new course? You can use this registration form to sign up for new study program. We will contact you as soon as possible with further information.</p>
<br />
<form name="RegForm" action="/submit.php" method="post">
<p>Name: <input type="text" size="65" name="Name" id="Name" required onblur="checkField1();"><br>
<span id="message1"></span></p>
<br />
<p>Address: <input type="text" size="65" name="Address" id="Address" required onblur="checkField2();"><br>
<span id="message2"></span></p>
<br />
<p>E-mail Address: <input type="text" size="65" name="EMail" id="Email" required onblur="checkField3();"><br>
<span id="message3"></span></p>
<br />
<p>Password: <input type="password" size="65" name="Password" id="Password" required onblur="checkField4();"><br>
<span id="message4"></span></p>
<br />
<p>Repeat Password: <input type="password" size="65" name="RepeatPassword" id="RepeatPassword" required onfocus="checkField5();" onblur="checkPassword();"><br>
<span id="message5"></span><br>
<span id="messagePWD"></span>
</p>
<br />
<p>Telephone: <input type="text" size="65" name="Telephone" id="Telephone" placeholder="090x xxx xxx" required onblur="checkField6();"><br>
<span id="message6"></span></p>
<br />
<p>
SELECT YOUR COURSE
<select type="text" value="" name="Subject">
<option>MBA</option>
<option>LLM</option>
<option>BA</option>
<option>MSc</option>
<option>PhD</option>
</select>
</p>
<br />
<br />
<p>Comments: <textarea cols="55" name="Comment"> </textarea></p>
<p>
<input type="submit" value="Submit" name="Submit" />
<input type="reset" value="Reset" name="Reset" />
</p>
<br>
<p>If you click the "Submit" button, the form-data will be sent to a page called "/submit.php".</p>
</br>
</form>
</body>
</html>
【问题讨论】:
-
var regex = /^[a-zA-Z\s]*$/; //Only letters allowed- 量词*表示 零 或更多,因此该表达式也允许 empty 值。请改用+,要求至少需要其中一个字符。 (例如,它仍然允许仅使用单个空格字符填充字段。如果您不希望这样 - 那么您不能只使用也允许空格的单个字符类。) -
非常感谢您的建议!欣赏它! :-)
标签: javascript html forms validation