我该如何修复表单验证

时间:2016-09-25 16:57:42

标签: javascript jquery forms

我的代码存在问题,如果您帮助我,我将不胜感激。问题是 - 当您正确填写表单中的所有输入时,脚本会从提交按钮中删除属性“已禁用”,但是例如,如果在填写表单后清除所有字段,则提交按钮将能够提交表单,但它必须返回属性“禁用”。我该如何解决?

//validation name
        document.callbackform.name.onkeyup = function() {
            var name = document.callbackform.name.value;
            if (name === "") {
                document.callbackform.name.removeAttribute("class", "ready");
                document.getElementById("callError").style.display = "block";
                document.getElementById("calllErrorTwo").style.display = "none";
            } else {
                    document.getElementById("callError").style.display = "none";
                    var pattern = new RegExp("^[а-я]+$", "i");
                    var isValid = this.value.search(pattern) >= 0;
                    if (!(isValid)) {
                        document.getElementById("calllErrorTwo").style.display = "block";
                        document.callbackform.name.removeAttribute("class", "ready");
                    } else {
                        document.getElementById("calllErrorTwo").style.display = "none";
                        document.callbackform.name.setAttribute("class", "ready");
                    }
            }
        };

        //validation phone
        document.callbackform.phone.onkeyup = function() {
            var name = document.callbackform.phone.value;
            if (name === "") {
                document.callbackform.phone.removeAttribute("class", "ready");
                document.getElementById("calltelError").style.display = "block";
                document.getElementById("calltelErrorTwo").style.display = "none";
            } else {
                    document.getElementById("calltelError").style.display = "none";
                    var pattern = new RegExp("[- +()0-9]+");
                    var isValid = this.value.search(pattern) >= 0;

                    if (!(isValid)) {
                        document.getElementById("calltelErrorTwo").style.display = "block";
                    } else {
                        document.getElementById("calltelErrorTwo").style.display = "none";
                        document.callbackform.phone.setAttribute("class", "ready");
                    }
                }
        };

        //filling the form
        document.callbackform.onkeyup = function() {
            var a = document.callbackform.name.getAttribute("class");
            var c = document.callbackform.phone.getAttribute("class");
            if (a === "ready" && c === "ready") {
                document.getElementById("subCallback").removeAttribute("disabled");
                document.getElementById("subCallback").style.cursor = "pointer";
            } else {
                document.getElementById("subCallback").setAttribute("disabled");
                document.getElementById("subCallback").style.cursor = "not-allowed";
            }
        };   

1 个答案:

答案 0 :(得分:1)

简单修复。 #if net45 System.Runtime.Serialization.Formatters.Binary; public class BinaryFormatSerializer : ISerializer { public string Serialize(object obj) { var serializer = new BinaryFormatter(); string binData = serializer.Serialize(obj); return new BinaryFormatter(binData).Format(); } } #else // Below you would put whatever logic // to not use 4.5 framework whenever // BinaryFormatter is added to core 不起作用,因为.setAttribute("disabled");是属性,而不是属性,因为它没有值。您只需使用disabled,如下所示:

.disabled = true;

使用以下内容删除已禁用的属性也是一件好事:

document.getElementById("subCallback").disabled = true;

请记住,document.getElementById("subCallback").disabled = false; 总是需要两个参数,第二个参数是属性值。