- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表单验证脚本,可以在用户提交时警告他们的表单存在问题。我想知道如何在用户键入并修复他们的错误时删除警告。例如,如果您提交一个空字段,会出现文本“不能为空”,我希望该文本在用户开始在输入字段中输入时立即消失,从而使该字段不再为空。任何帮助都会受到赞赏,并且会大有帮助。谢谢!
HTML
<!-- Individual Form -->
<form id="individual" action="#" method="POST" onsubmit="return validateStudentSignUpForm()" autocomplete="off">
<!-- Individual First Name -->
<div class="form-group">
<span id="firstid" class="text-warning"></span>
<input class="form-control" id="first" name="first" type="text" placeholder="First name" />
</div>
<!-- Individual Last Name -->
<div class="form-group">
<span id="lastid" class="text-warning"></span>
<input class="form-control" id="last" name="last" type="text" placeholder="Last name" />
</div>
<!-- Individual Email -->
<div class="form-group">
<span id="email1id" class="text-warning"></span>
<input class="form-control email" id="email1" name="email" type="text" placeholder="Email" />
</div>
<!-- Individual Password -->
<div class="form-group">
<span id="password1id" class="text-warning"></span>
<input class="form-control" id="password1" name="password" type="password" placeholder="Password" />
</div>
<!-- Individual's Birthday -->
<div class="form-group">
<label>Birthday</label>
<div class="form-inline">
<!-- Month -->
<select id="month1" name="month" onChange="changeDate1(this.options[selectedIndex].value);" class="form-control month-space">
<option value="na">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<!-- Day -->
<select name="day" id="day1" class="form-control day-space">
<option value="na">Day</option>
</select>
<!-- Year -->
<select name="year" id="year1" class="form-control year-space">
<option value="na">Year</option>
</select>
<span id="date1id" class="text-warning"></span>
</div>
</div>
<button type="submit" name="submit" class="btn blue-button">Confirm</button>
</form>
<!-- Parent Form -->
<form id="parent" class="hidden" action="#" method="POST" onsubmit="return validateStudentSignUpForm()" autocomplete="off">
<!-- Parent's First Name -->
<div class="form-group">
<span id="parent-firstid" class="text-warning"></span>
<input class="form-control" id="parent-first" name="parent-first" type="text" placeholder="Parent's first name" />
</div>
<!-- Parent's Last Name -->
<div class="form-group">
<span id="parent-lastid" class="text-warning"></span>
<input class="form-control" id="parent-last" name="parent-last" type="text" placeholder="Parent's last name" />
</div>
<!-- Parent Email -->
<div class="form-group">
<span id="email2id" class="text-warning"></span>
<input class="form-control" id="email2" name="email" type="text" placeholder="Email" />
</div>
<!-- Parent Password -->
<div class="form-group">
<span id="password2id" class="text-warning"></span>
<input class="form-control" id="password2" name="password" type="password" placeholder="Password" />
</div>
<!-- Child's First Name -->
<div class="form-group">
<span id="child-firstid" class="text-warning"></span>
<input class="form-control" id="child-first" name="child-first" type="text" placeholder="Child's first name" />
</div>
<!-- Child's Birthday -->
<div class="form-group">
<label>Child's birthday</label>
<div class="form-inline">
<!-- Month -->
<select id="month2" name="month" onChange="changeDate2(this.options[selectedIndex].value);" class="form-control month-space">
<option value="na">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<!-- Day -->
<select name="day" id="day2" class="form-control day-space">
<option value="na">Day</option>
</select>
<!-- Year -->
<select name="year" id="year2" class="form-control year-space">
<option value="na">Year</option>
</select>
<span id="date2id" class="text-warning"></span>
</div>
</div>
<button type="submit" name="submit" class="btn blue-button">Confirm</button>
</form>
<p class="text-center" id="small">By signing up you agree to our
<a href="#">Terms of Use</a> and
<a href="#">Privacy Policy</a>
</p>
</div>
</div>
</div>
JavaScript
function validateStudentSignUpForm() {
var first = document.getElementById("first").value;
var last = document.getElementById("last").value;
var email1 = document.getElementById("email1").value;
var password1 = document.getElementById("password1").value;
var parentFirst = document.getElementById("parent-first").value;
var parentLast = document.getElementById("parent-last").value;
var childFirst = document.getElementById("child-first").value;
var email2 = document.getElementById("email2").value;
var password2 = document.getElementById("password2").value;
var month1 = document.getElementById("month1").value;
var day1 = document.getElementById("day1").value;
var year1 = document.getElementById("year1").value;
var month2 = document.getElementById("month2").value;
var day2 = document.getElementById("day2").value;
var year2 = document.getElementById("year2").value;
var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var nameFilter = /^([^0-9]*)$/;
// First name can't be blank
if (first == "") {
document.getElementById("first").className = document.getElementById("first").className + " error";
document.getElementById("firstid").innerHTML = "Can't be blank";
}
// First name can't be a number
else if (!nameFilter.test(first)) {
document.getElementById("first").className = document.getElementById("first").className + " error";
document.getElementById("firstid").innerHTML = "Can't have numbers";
}
// First name can't be longer than 50 characters
else if (first.length > 50) {
document.getElementById("first").className = document.getElementById("first").className + " error";
document.getElementById("firstid").innerHTML = "Name is too long";
}
// Last name can't be blank
if (last == "") {
document.getElementById("last").className = document.getElementById("last").className + " error";
document.getElementById("lastid").innerHTML = "Can't be blank";
}
// Last name can't be a number
else if (!nameFilter.test(last)) {
document.getElementById("last").className = document.getElementById("last").className + " error";
document.getElementById("lastid").innerHTML = "Can't have numbers";
}
// Last name can't be longer than 50 characters
else if (last.length > 50) {
document.getElementById("last").className = document.getElementById("last").className + " error";
document.getElementById("lastid").innerHTML = "Name is too long";
}
// Email can't be blank
if (email1 == "") {
document.getElementById("email1").className = document.getElementById("email1").className + " error";
document.getElementById("email1id").innerHTML = "Can't be blank";
}
// Email test
else if (!emailFilter.test(email1)) {
document.getElementById("email1").className = document.getElementById("email1").className + " error";
document.getElementById("email1id").innerHTML = "Use a proper email format";
}
// Password can't be blank
if (password1 == "") {
document.getElementById("password1").className = document.getElementById("password1").className + " error";
document.getElementById("password1id").innerHTML = "Can't be blank";
}
// Password has to be 6 characters
else if (password1.length < 6) {
document.getElementById("password1").className = document.getElementById("password1").className + " error";
document.getElementById("password1id").innerHTML = "Password must be 6 characters long";
}
// Password can't be longer than 100 characters
else if (password1.length > 100) {
document.getElementById("password1").className = document.getElementById("password1").className + " error";
document.getElementById("password1id").innerHTML = "Password must be less than 100 characters long";
}
// Parent first can't be blank
if (parentFirst == "") {
document.getElementById("parent-first").className = document.getElementById("parent-first").className + " error";
document.getElementById("parent-firstid").innerHTML = "Can't be blank";
}
// Parent first can't be a number
else if (!nameFilter.test(parentFirst)) {
document.getElementById("parent-first").className = document.getElementById("parent-first").className + " error";
document.getElementById("parent-firstid").innerHTML = "Can't have numbers";
}
// Parent first can't be longer than 50 characters
else if (parentFirst.length > 50) {
document.getElementById("parent-first").className = document.getElementById("parent-first").className + " error";
document.getElementById("parent-firstid").innerHTML = "Name is too long";
}
// Parent last can't be blank
if (parentLast == "") {
document.getElementById("parent-last").className = document.getElementById("parent-last").className + " error";
document.getElementById("parent-lastid").innerHTML = "Can't be blank";
}
// Parent last can't be a number
else if (!nameFilter.test(parentLast)) {
document.getElementById("parent-last").className = document.getElementById("parent-last").className + " error";
document.getElementById("parent-lastid").innerHTML = "Can't have numbers";
}
// Parent last can't be longer than 50 characters
else if (parentLast.length > 50) {
document.getElementById("parent-last").className = document.getElementById("parent-last").className + " error";
document.getElementById("parent-lastid").innerHTML = "Name is too long";
}
// Child first can't be blank
if (childFirst == "") {
document.getElementById("child-first").className = document.getElementById("child-first").className + " error";
document.getElementById("child-firstid").innerHTML = "Can't be blank";
}
// Child first can't be a number
else if (!nameFilter.test(childFirst)) {
document.getElementById("child-first").className = document.getElementById("child-first").className + " error";
document.getElementById("child-firstid").innerHTML = "Can't have numbers";
}
// Child first can't be longer than 50 characters
else if (childFirst.length > 50) {
document.getElementById("child-first").className = document.getElementById("child-first").className + " error";
document.getElementById("child-firstid").innerHTML = "Name is too long";
}
// Email can't be blank
if (email2 == "") {
document.getElementById("email2").className = document.getElementById("email2").className + " error";
document.getElementById("email2id").innerHTML = "Can't be blank";
}
// Email2 test
else if (!emailFilter.test(email2)) {
document.getElementById("email2").className = document.getElementById("email2").className + " error";
document.getElementById("email2id").innerHTML = "Use a proper email format";
}
// Password can't be blank
if (password2 == "") {
document.getElementById("password2").className = document.getElementById("password2").className + " error";
document.getElementById("password2id").innerHTML = "Can't be blank";
}
// Password has to be 6 characters
else if (password2.length < 6) {
document.getElementById("password2").className = document.getElementById("password2").className + " error";
document.getElementById("password2id").innerHTML = "Password must be 6 characters long";
}
// Password can't be longer than 100 characters
else if (password2.length > 100) {
document.getElementById("password2").className = document.getElementById("password2").className + " error";
document.getElementById("password2id").innerHTML = "Password must be less than 100 characters long";
}
// Month1 can't be default
if (month1 === "na") {
document.getElementById("month1").className = document.getElementById("month1").className + " error";
document.getElementById("date1id").innerHTML = "Choose a valid date";
}
// Day1 can't be default
if (day1 === "na") {
document.getElementById("day1").className = document.getElementById("day1").className + " error";
document.getElementById("date1id").innerHTML = "Choose a valid date";
}
// Month1 can't be default
if (year1 === "na") {
document.getElementById("year1").className = document.getElementById("year1").className + " error";
document.getElementById("date1id").innerHTML = "Choose a valid date";
}
// Month2 can't be default
if (month2 === "na") {
document.getElementById("month2").className = document.getElementById("month2").className + " error";
document.getElementById("date2id").innerHTML = "Choose a valid date";
}
// Day2 can't be default
if (day2 === "na") {
document.getElementById("day2").className = document.getElementById("day2").className + " error";
document.getElementById("date2id").innerHTML = "Choose a valid date";
}
// Year2 can't be default
if (year2 === "na") {
document.getElementById("year2").className = document.getElementById("year2").className + " error";
document.getElementById("date2id").innerHTML = "Choose a valid date";
}
return false;
}
.error {
border: 2px solid red;
}
最佳答案
将 onfocus
事件添加到您的输入字段,这将删除验证警告消息
关于javascript - 如何在用户键入时更新 JavaScript 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50283804/
在 JSF2 应用程序中遇到验证属性的问题时,有两种主要方法。 使用 Annotation 在 ManagedBean 上定义验证 @ManagedBean public class MyBean {
我想实现一个不常见的功能,我认为 jquery 验证插件将是最好的方法(如果您在没有插件的情况下建议和回答,我们也会欢迎)。我想在用户在输入字段中输入正确的单词后立即隐藏表单。我试过这个: $("
我有几个下拉菜单(类名为month_dropdown),并且下拉菜单的数量不是恒定的。我怎样才能为它们实现 NotEqual 验证。我正在使用 jQuery 验证插件。 这就是我写的 - jQuery
我设法制作了这个网址验证代码并且它起作用了。但我面临着一个问题。我认为 stackoverflow 是获得解决方案的最佳场所。 function url_followers(){ var url=do
我目前正在使用后端服务,该服务允许用户在客户端应用程序上使用 Google Games 库登录。 用户可以通过他们的 gplay ID 向我们发送信息,以便登录或恢复旧帐户。用户向我们发送以下内容,包
我正在尝试验证输入以查看它是否是有效的 IP 地址(可能是部分地址)。 可接受的输入:172、172.112、172.112.113、172.112.113.114 Not Acceptable 输入
我从 Mongoose 验证中得到这条消息: 'Validator failed for path phone with value ``' 这不应该发生,因为不需要电话。 这是我的模型架构: var
我一直在尝试使用Python-LDAP (版本 2.4.19)在 MacOS X 10.9.5 和 Python 2.7.9 下 我想在调用 .start_tls_s() 后验证与给定 LDAP 服务
我正在处理一个仅与 IE6 兼容的旧 javascript 项目(抱歉...),我想仅在 VS 2017 中禁用此项目的 ESLint/CSLint/Javascript 验证/CSS 验证。 我知道
我正在寻找一种方法来验证 Spring 命令 bean 中的 java.lang.Double 字段的最大值和最小值(一个值必须位于给定的值范围之间),例如, public final class W
我正在尝试在 springfuse(JavaEE 6 + Spring Framework (针对 Jetty、Tomcat、JBoss 等)) 和 maven 的帮助下构建我的 webapps 工作
我试图在我们的项目中使用 scalaz 验证,但遇到了以下情况: def rate(username: String, params: Map[String, String]): Validation
我有一个像这样的 Yaml 文件 name: hhh_aaa_bbb arguments: - !argument name: inputsss des
我有一个表单,人们可以单击并向表单添加字段,并且我需要让它在单击时验证这些字段中的值。 假设我单击它两次并获取 2 个独立的字段集,我需要旋转 % 以确保它在保存时等于 100。 我已放入此函数以使其
在我的页面中有一个选项可以创建新的日期字段输入框。用户可以根据需要创建尽可能多的“截止日期”和“起始日期”框。就像, 日期_to1 || date_from1 日期到2 ||日期_from2 date
我有一个像这样的 Yaml 文件 name: hhh_aaa_bbb arguments: - !argument name: inputsss des
有没有办法在动态字段上使用 jquery 验证表单。 我想将其设置为必填字段 我正在使用 Jsp 动态创建表单字段。 喜欢 等等...... 我想使用必需的表单字段验证此表单字段。 最佳答
嗨,任何人都可以通过提供 JavaScript 代码来帮助我验证用户名文本框不应包含数字,它只能包含一个字符。 最佳答案 使用正则表达式: (\d)+ 如果找到匹配项,则字符串中就有一个数字。 关于J
我有两个输入字段holidayDate和Description(id=tags) $(document).ready(function() {
我遇到了这个问题,这些验证从电子邮件验证部分开始就停止工作。 我只是不明白为什么即使经过几天的观察,只是想知道是否有人可以在这里指出我的错误? Javascript部分: function valid
我是一名优秀的程序员,十分优秀!