javascript - 完成必填字段后,如何允许提交表单?
问题描述
我已经完成了带有即时字段验证的代码,但如果字段没有值,我也想阻止提交表单。我已经设法阻止提交表单,但是当我返回并提供一个值时,它仍然不允许它提交。知道我在这里做错了什么吗?
这是HTML:
<form id="contactForm" action="https://formspree.io/xrggbejj" method="POST" onsubmit="return formValidation(contactForm);">
<div class="row">
<div class="col">
<label for="fName">First Name</label>
</div>
<div class="col">
<input type="text" id="fName" name="firstname" onblur="checkFirstName(firstName)" autofocus/>
<img src="../images/alert-icon.png" class="alert" id="firstNameMsg" alt="alert icon" width="40" height="35"/>
</div>
</div> <!-- End of row -->
<div class="row">
<div class="col">
<label for="lName">Last Name</label>
</div>
<div class="col">
<input type="text" id="lName" name="lastname" onblur="checkLastName(lastName)"/>
<img src="../images/alert-icon.png" class="alert" id="lastNameMsg" alt="alert icon" width="40" height="35"/>
</div>
</div> <!-- End of row -->
<div class="row">
<div class="col">
<label for="subject">Questions and/or Comments</label>
</div>
<div class="col">
<textarea id="subject" name="subject" onblur="checkSubject(subject)" style="height:200px"></textarea>
<img src="../images/alert-icon.png" class="alert" id="subjectMsg" alt="alert icon" width="40" height="35"/>
</div>
</div> <!-- End of row -->
<div class="row">
<input type="submit" value="SEND" />
</div> <!-- End of row -->
</form>
这是JavaScript:
联系表单字段验证
var alertStyle;
var alertImg;
const firstName = document.forms['contactForm']['fName'];
const lastName = document.forms['contactForm']['lName'];
const subject = document.forms['contactForm']['subject'];
function checkFirstName(firstName) {
if (firstName.value === '') {
alertStyle = document.getElementById('fName').style.border = '2px solid rgba(255, 6, 0)';
alertImg = document.getElementById('firstNameMsg').style.display = 'inline-block';
return false;
} else if (firstName.value !== '') {
alertStyle = document.getElementById('fName').style.border = '1px solid #ccc';
alertImg = document.getElementById('firstNameMsg').style.display = 'none';
return true;
}
}
function checkLastName(lastName) {
if (lastName.value === '') {
alertStyle = document.getElementById('lName').style.border = '2px solid rgba(255, 6, 0)';
alertImg = document.getElementById('lastNameMsg').style.display = 'inline-block';
return false;
} else if (lastName.value !== '') {
alertStyle = document.getElementById('lName').style.border = '1px solid #ccc';
alertImg = document.getElementById('lastNameMsg').style.display = 'none';
return true;
}
}
function checkSubject(subject) {
if (subject.value === '') {
alertStyle = document.getElementById('subject').style.border = '2px solid rgba(255, 6, 0)';
alertImg = document.getElementById('subjectMsg').style.display = 'inline-block';
return false;
} else if (firstName.value !== '') {
alertStyle = document.getElementById('subject').style.border = '1px solid #ccc';
alertImg = document.getElementById('subjectMsg').style.display = 'none';
return true;
}}
提交时联系表单验证
function formValidation(contactForm) {
if (firstName && lastName && subject !== true) {
alert('Please fill out required fields.')
event.preventDefault()
return false;
} else if (firstName && lastName && subject === true) {
return true;
}
}
解决方案
、firstName
和变量指向输入而不是它们内部的实际值lastName
。subject
您可以将您的功能修改为
function formValidation(contactForm) {
if (!firstName.value || !lastName.value || !subject.value) {
alert('Please fill out required fields.')
event.preventDefault()
return false;
} else {
return true;
}
}
在这里,我使用!
运算符将输入转换为何True
时为空,如果任何输入为空,则第一种情况将评估,否则您的函数将返回 true。
推荐阅读
- excel - 在特定位置插入行
- java - 在列表视图中保存复选框的状态
- java - 带有 Fragment 的 Fragment 中的 TabLayout
- xml - 如何在 XSLT 中应用多个导入以生成 xml
- javascript - Javascript 根据新日期仅获得偶数小时周期
- java - 如何获取队列中元素的位置
- javascript - opencv4nodejs 保存图像的描述符并加载备份
- java - Java 声纳违反“将三元运算符的这种用法转换为“if”/“else”结构”
- maven - 有没有办法控制 Surefire 插件的报告?
- mysql - 关于 MySQL Entries [SAAS 结构]