简体   繁体   English

JavaScript-如何隐藏表单验证错误

[英]JavaScript - How to hide errors on Form Validation

How do I hide the error(s) messages and the red border when a user corrects their errors on my form? 用户更正我的表单中的错误时,如何隐藏错误消息和红色边框? At the moment it will keep showing the error(s) until the page is refreshed or submitted. 目前,它将一直显示错误,直到刷新或提交页面为止。

function checkForm() {
            var valid = true;

            if (!retext.test(document.myform.textfield.value)) {
                document.myform.textfield.style.border = "3.5px solid red";
                document.getElementById("text").innerHTML = "Invalid text.";
                document.getElementById("text").style.display = "block";
                valid = false;
            }

            if (!re.test(document.myform.email.value)) {
                document.myform.email.style.border = "3.5px solid red";
                document.getElementById("emailwarn").innerHTML = "Invalid email.";
                document.getElementById("emailwarn").style.display = "block";
                valid = false;
            }

            if (!retel.test(document.myform.tel.value)) {
                document.myform.tel.style.border = "3.5px solid red";
                document.getElementById("telwarn").innerHTML = "Invalid telephone number.";
                document.getElementById("telwarn").style.display = "block";
                valid = false;
            }

            return valid;
        }

I really have no idea on how to do this. 我真的不知道该怎么做。

Assuming the checkForm function is called each time the form is amended you can include else clauses to reset the style and visible state of the warnings 假设每次修改表单时都会调用checkForm函数,则可以包含else子句以重置警告的样式和可见状态

function checkForm() {
    var valid = true;

    if (!retext.test(document.myform.textfield.value)) {
        document.myform.textfield.border = "3.5px solid red";
        document.getElementById("text").innerHTML = "Invalid text.";
        document.getElementById("text").style.display = "block";
        valid = false;
    } else {
        document.myform.textfield.border = 0;
        document.getElementById("text").style.display = "none";
    }

    if (!re.test(document.myform.email.value)) {
        document.myform.email.style.border = "3.5px solid red";
        document.getElementById("emailwarn").innerHTML = "Invalid email.";
        document.getElementById("emailwarn").style.display = "block";
        valid = false;
    } else {
        document.myform.email.border = 0;
        document.getElementById("emailwarn").style.display = "none";
    }

    if (!retel.test(document.myform.tel.value)) {
        document.myform.tel.style.border = "3.5px solid red";
        document.getElementById("telwarn").innerHTML = "Invalid telephone number.";
        document.getElementById("telwarn").style.display = "block";
        valid = false;
    } else {
        document.myform.tel.border = 0;
        document.getElementById("telwarn").style.display = "none";
    }

    return valid;
}

Just add code which clears up the error messages at the beginning of your checkForm() function. 只需在checkForm()函数的开头添加清除错误消息的代码即可。

In your current code if error messages had been set once they have no chance to be changed on the correct input. 在您当前的代码中,如果错误消息一旦被设置,就没有机会在正确的输入上进行更改。

For example, 例如,

function clearErrors() {
  document.getElementById("text").style.display      = "none";
  document.getElementById("emailwarn").style.display = "none";
  document.getElementById("telwarn").style.display   = "none";
}

function checkForm() {      
  var valid = true;

  clearErrors();

  ...

Well first it would be better to seperate javascript and CSS styles, by applying classes: 首先,最好通过应用类来分隔javascript和CSS样式:

input.error {
  border: 3.5px solid red;
}

To apply the style: 要应用样式:

document.myform.textfield.classList.add("error");

To remove the style: 删除样式:

document.myform.textfield.classList.remove("error");

To hide the warn element use: 要隐藏警告元素,请使用:

document.getElementById("telwarn").style.display = "none";

Beware that classList does not work with ie < 10 注意classList不适用于<10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM