【问题标题】:On submit text box deleted? [duplicate]提交时删除文本框? [复制]
【发布时间】:2014-05-07 08:50:17
【问题描述】:

我正在尝试在提交时删除<span class="reqMsg">* Email is required</span,如果它验证。例如,如果在第一个字段框中没有输入任何内容,则显示错误,如果显示了某些内容,我该如何消除错误?

My JSFIDDLE:

MY JS:

function elem(id) {
    return document.getElementById(id);
};


window.onload = function () {
    document.querySelector("#RadioGroup1_0").click();


    var form = document.getElementById('form');
    form.onsubmit = function (e) {
        var rules = [
        ['first-name', elem('first-name').value.length > 0],
        ['last-name', elem('last-name').value.length > 0],
        ['email', elem('email').value.length > 0 && /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(elem('email').value)],
        ['phone', elem('phone').value.length > 7 && elem('phone').value.length < 11 && /^(\+\d{1,2})?[\d ()-]+$/.test(elem('phone').value)]

    ];

    function myFunction()
{
document.getElementById("myForm").reset();
}

        function alpha(e) {
            var k;
            document.all ? k = e.keyCode : k = e.which;
            return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8);
        }

        var valid = true;
        var firstFocus = null;
        for (var i = 0; i < rules.length; i++) {
            if (!rules[i][1]) {
                valid = false;
                var parent = elem(rules[i][0]).parentNode;
                parent.children[2].style.display = "inline";
                if (firstFocus == null) firstFocus = parent.children[1];
            }
        }
        if (!valid) {
            firstFocus.focus();
            return false;
        }
        return true;
    };
}


function onlyAlphabets(e, t) {
    try {
        if (window.event) {
            var charCode = window.event.keyCode;
        } else if (e) {
            var charCode = e.which;
        } else {
            return true;
        }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode === 8)) return true;
        else return false;
    } catch (err) {
        alert(err.Description);
    }
}


var specialKeys = new Array();
specialKeys.push(8); //Backspace
function IsNumeric(e) {
    var keyCode = e.which ? e.which : e.keyCode
    var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
    //document.getElementById("phone").style.display = ret ? "none" : "inline";
    return ret;
}

//var valid = email.match(/^[a-zA-Z0-9\.]+@([a-zA-Z0-9\.-]){1,}\.[a-z]{2,4}$/);

【问题讨论】:

标签: javascript jquery html validation


【解决方案1】:

form.onsubmit,成功的return true语句之前插入以下行

document.querySelector(".reqMsg").style.display = "none";

它将隐藏错误消息。

如果你想隐藏所有的错误信息

var errorMessages = document.querySelectorAll(".reqMsg");
for(var dom in errorMessages) { 
  errorMessages[dom].style.display = "none";
}

如果您也想清除错误消息,请使用

document.querySelector(".reqMsg").innerHTML = ""

对于单个字段验证:

 for (var i = 0; i < rules.length; i++) {
            var parent = elem(rules[i][0]).parentNode;
            if (!rules[i][1]) {
                valid = false;
                parent.children[2].style.display = "inline";
                if (firstFocus == null) firstFocus = parent.children[1];
            } else {
                parent.children[2].style.display = "none";
            }

        }

希望对你有用:)

【讨论】:

  • 您好,非常感谢您帮助我!虽然只有名字字段框不会重置其他内容?
  • document.querySelectorAll(".reqMsg").style.display = "none"; 使用下面的语句
  • 这完全停止了工作,现在它自己的字段框旁边的所有错误都消失了:(。
  • 我已经更新了答案,请查收。将替换该行替换为以下代码var errorMessages = document.querySelectorAll(".reqMsg"); for(var dom in errorMessages) { errorMessages[dom].style.display = "none"; }
  • 您好,似乎仍然无法正常工作:/您可以在 JSFIDDLE 中编辑它吗
【解决方案2】:

成功时给 span 类这个 css display : none

【讨论】:

  • 你能说得更具体一点吗?
  • 好吧,在您的电子邮件验证功能中,您还可以添加 $(".reqmsg").css("display", "none");隐藏跨度
  • 似乎不起作用:/
  • $(".reqMsg").css({display: "none"});这对我有用..并且在样式表中确实包含 .reqMsg { display :block;}
  • 你在哪里输入代码?
【解决方案3】:

据我了解,这是你需要的

if (!rules[i][1]) {
    valid = false;
    var parent = elem(rules[i][0]).parentNode;
    parent.children[2].style.display = "inline";
    if (firstFocus == null) firstFocus = parent.children[1];
} else {
    parent.children[2].style.display = "none";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2014-04-15
    • 2012-10-10
    • 1970-01-01
    • 2020-06-19
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多