2016-09-25 2 views
1

私のコードに問題があります。問題は、フォームのすべての入力を正しく入力すると、スクリプトはサブミット・ボタンから属性「disabled」を削除しますが、フォームに記入した後にすべてのフィールドをクリアすると、フォームをサブミットできます。属性「無効」を戻さなければならない。どうすれば修正できますか?フォームの検証を修正する方法

//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

シンプルな修正。 .setAttribute("disabled");は機能しません。disabledは値ではないため属性ではなくプロパティです。あなたは、単に示すよう.disabled = true;を使用する必要があります。

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

また、disabledプロパティを削除するには、次を使用すると良いでしょう。

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

覚えておいてください、setAttribute()は常に2つの引数を必要とし、2番目の引数は属性値です。

関連する問題