使用 JS 验证客户端表单上的条目时出现问题 (HTML)
Problem using JS to validate entry on client-side form (HTML)
我在验证以下表单时遇到问题运行。
每个输入应该显示:
a) 红色边框和红色感叹号圈(输入后出现)为错误输入。
b) 或者如果输入正确则设置绿色边框和绿色勾选圆圈(输入后出现)。
谁能help/spot我做错了什么?
谢谢大家
HTML
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/b9f255870f.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="css/fontawesome/all.css">
<link rel="stylesheet" href="style.css">
<link rel="JavaScript" href="js/script.js">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Business Contact Us</title>
</head>
<body>
<section class="main_body">
<!-- Merch Section & Folder Structure-->
<div class="form-body">
<div id="form-error"></div>
<div class="container">
<form id="contactus_form" class="contactus_form">
<div class="form-control">
<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="lastName">Last Name</label>
<input type="text" id="lastName" name="lastName">
<i class="fas fa-check-circle "></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="companyPosition">Position</label>
<input type="text" id="companyPosition" name="companyPosition">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="email" id="email" name="email">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control ">
<label for="message">Message</label>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<button class="contactus-form-submitbtn" type="submit">Submit</button>
</form>
</div>
</div>
</section>
</body>
</html>
CSS
.form-body {
min-height: 1300px;
width: 100%;
background-color: grey;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 0px;
}
.container {
width: 90%;
max-width: 700px;
margin: 50px auto;
padding: 20px 40px;
box-shadow: 0px 0px 20px #00000010;
background-color: white;
border-radius: 8px;
margin-bottom: 20px;
}
.contactus-form {
padding: 30px 40px;
}
.form-control {
margin-bottom: 10px;
padding-bottom: 20px;
position: relative;
border: none;
}
.form-control label {
display: inline-block;
margin-bottom: 3px;
font-size: 18 px;
}
.form-control input {
border: 2px solid #f0f0f0;
border-radius: 4px;
display: block;
font-family: inherit;
font-size: 14px;
padding: 10px;
width: 100%;
}
.form-control.success input {
border-color: #2ecc71;
}
.form-control.error input {
border-color: #e74c3c;
}
.form-control textarea {
width: 100%;
padding: 5px;
font-size: 18px;
border: 1px solid rgb(128, 128, 128, 0.199);
margin-top: 5px;
}
.form-control i {
position: absolute;
top: 40px;
right: 10px;
visibility: hidden;
}
.form-control.success i.fa-check-circle {
color: #2ecc71;
visibility: visible;
}
.form-control.error i.fa-exclamation-circle {
color: #e74c3c;
visibility: visible;
}
.form-control small {
visibility: hidden;
position: absolute;
bottom: 0;
left: 0;
}
.contactus-form-submitbtn {
width: 100%;
height: 30px;
margin: 10px 0px;
color: #ffffff;
display: block;
background-color: rgb(31, 136, 229);
border: 1px solid rgb(128, 128, 128, 0.199);
border-radius: 2px;
}
label#disclaimer-label {
font-size: 12px;
width: 600px;
margin: 0px;
}
.form-group-disclaimer {
padding: 10px 0px;
}
JS
const contactus_form = document.getElementById('contactus_form');
const firstName = document.getElementById('firstName');
const lastName = document.getElementById('lastName');
const companyPosition = document.getElementsById('companyPosition');
const email = document.getElementById('email');
const message = document.getElementById('message');
contactus_form.addEventListener('submit', (e) => {
e.preventDefault();
checkInputs();
});
function checkInputs() {
const firstNameValue = firstName.value.trim();
const lastNameValue = lastName.value.trim();
const companyPositionValue = companyPosition.value.trim();
const emailValue = email.value.trim();
const messageValue = message.value.trim();
if (firstNameValue === '') {
setErrorFor(firstNameValue, 'First name cannot be blank');
} else {
setSuccessFor(firstNameValue);
}
if (lastNameValue === '') {
setErrorFor(lastNameValue, 'Last name cannot be blank');
} else {
setSuccessFor(lastNameValue);
}
if (companyPositionValue === '') {
setErrorFor(companyPositionValue, 'Company position cannot be blank');
} else {
setSuccessFor(companyPositionValue);
}
if (emailValue === '') {
setErrorFor(emailValue, 'Email cannot be blank');
} else if (!isEmail(emailValue)) {
setErrorFor(emailValue, 'Not a valid email')
} else {
setSuccessFor(emailValue);
}
if (messageValue === '') {
setErrorFor(messageValue, 'Message cannot be blank');
} else {
setSuccessFor(messageValue);
}
}
function setErrorFor(input, message) {
const formControl = input.parentElement;
const small = formControl.querySelector('small');
small.innerText = message;
formControl.className = 'form-control error';
}
function setSuccessFor(input, message) {
const formControl = input.parentElement;
formControl.className = 'form-control success';
}
function isEmail(email) {
return /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
您的每个 ID 属性中都有空格,例如:
id="companyPosition "
尝试删除它们,看看它是否有效。
我在验证以下表单时遇到问题运行。
每个输入应该显示:
a) 红色边框和红色感叹号圈(输入后出现)为错误输入。
b) 或者如果输入正确则设置绿色边框和绿色勾选圆圈(输入后出现)。
谁能help/spot我做错了什么?
谢谢大家
HTML
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;500&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/b9f255870f.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="css/fontawesome/all.css">
<link rel="stylesheet" href="style.css">
<link rel="JavaScript" href="js/script.js">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Business Contact Us</title>
</head>
<body>
<section class="main_body">
<!-- Merch Section & Folder Structure-->
<div class="form-body">
<div id="form-error"></div>
<div class="container">
<form id="contactus_form" class="contactus_form">
<div class="form-control">
<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="lastName">Last Name</label>
<input type="text" id="lastName" name="lastName">
<i class="fas fa-check-circle "></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="companyPosition">Position</label>
<input type="text" id="companyPosition" name="companyPosition">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control">
<label for="email">Email</label>
<input type="email" id="email" name="email">
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<div class="form-control ">
<label for="message">Message</label>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<i class="fas fa-check-circle"></i>
<i class="fas fa-exclamation-circle"></i>
<small>Error Message</small>
</div>
<button class="contactus-form-submitbtn" type="submit">Submit</button>
</form>
</div>
</div>
</section>
</body>
</html>
CSS
.form-body {
min-height: 1300px;
width: 100%;
background-color: grey;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 0px;
}
.container {
width: 90%;
max-width: 700px;
margin: 50px auto;
padding: 20px 40px;
box-shadow: 0px 0px 20px #00000010;
background-color: white;
border-radius: 8px;
margin-bottom: 20px;
}
.contactus-form {
padding: 30px 40px;
}
.form-control {
margin-bottom: 10px;
padding-bottom: 20px;
position: relative;
border: none;
}
.form-control label {
display: inline-block;
margin-bottom: 3px;
font-size: 18 px;
}
.form-control input {
border: 2px solid #f0f0f0;
border-radius: 4px;
display: block;
font-family: inherit;
font-size: 14px;
padding: 10px;
width: 100%;
}
.form-control.success input {
border-color: #2ecc71;
}
.form-control.error input {
border-color: #e74c3c;
}
.form-control textarea {
width: 100%;
padding: 5px;
font-size: 18px;
border: 1px solid rgb(128, 128, 128, 0.199);
margin-top: 5px;
}
.form-control i {
position: absolute;
top: 40px;
right: 10px;
visibility: hidden;
}
.form-control.success i.fa-check-circle {
color: #2ecc71;
visibility: visible;
}
.form-control.error i.fa-exclamation-circle {
color: #e74c3c;
visibility: visible;
}
.form-control small {
visibility: hidden;
position: absolute;
bottom: 0;
left: 0;
}
.contactus-form-submitbtn {
width: 100%;
height: 30px;
margin: 10px 0px;
color: #ffffff;
display: block;
background-color: rgb(31, 136, 229);
border: 1px solid rgb(128, 128, 128, 0.199);
border-radius: 2px;
}
label#disclaimer-label {
font-size: 12px;
width: 600px;
margin: 0px;
}
.form-group-disclaimer {
padding: 10px 0px;
}
JS
const contactus_form = document.getElementById('contactus_form');
const firstName = document.getElementById('firstName');
const lastName = document.getElementById('lastName');
const companyPosition = document.getElementsById('companyPosition');
const email = document.getElementById('email');
const message = document.getElementById('message');
contactus_form.addEventListener('submit', (e) => {
e.preventDefault();
checkInputs();
});
function checkInputs() {
const firstNameValue = firstName.value.trim();
const lastNameValue = lastName.value.trim();
const companyPositionValue = companyPosition.value.trim();
const emailValue = email.value.trim();
const messageValue = message.value.trim();
if (firstNameValue === '') {
setErrorFor(firstNameValue, 'First name cannot be blank');
} else {
setSuccessFor(firstNameValue);
}
if (lastNameValue === '') {
setErrorFor(lastNameValue, 'Last name cannot be blank');
} else {
setSuccessFor(lastNameValue);
}
if (companyPositionValue === '') {
setErrorFor(companyPositionValue, 'Company position cannot be blank');
} else {
setSuccessFor(companyPositionValue);
}
if (emailValue === '') {
setErrorFor(emailValue, 'Email cannot be blank');
} else if (!isEmail(emailValue)) {
setErrorFor(emailValue, 'Not a valid email')
} else {
setSuccessFor(emailValue);
}
if (messageValue === '') {
setErrorFor(messageValue, 'Message cannot be blank');
} else {
setSuccessFor(messageValue);
}
}
function setErrorFor(input, message) {
const formControl = input.parentElement;
const small = formControl.querySelector('small');
small.innerText = message;
formControl.className = 'form-control error';
}
function setSuccessFor(input, message) {
const formControl = input.parentElement;
formControl.className = 'form-control success';
}
function isEmail(email) {
return /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
您的每个 ID 属性中都有空格,例如:
id="companyPosition "
尝试删除它们,看看它是否有效。