2017-02-22 1 views
0

appendChildを使用して行を追加しました。appendChildのフォーム検証

addrowボタンをクリックすると、行が形成されますが、フォームの検証はその行に適用されません。そして今、私はすべての行の検証が必要です。方法をお勧めします。

は、私は多くの問題がここにあります。他のすべてのアドオン行

var counter = 0; 
 
var limit = 0/0; 
 
function addInput(dynamicInput) { 
 
    if (counter == limit) { 
 
     alert("You have reached the limit of adding " + counter + " inputs"); 
 
    } 
 
    else { 
 
     var newdiv = document.createElement('div'); 
 
     newdiv.innerHTML = "<br>User:" + " <input type='text' name='user' id='user'> " + 
 
    "Age:" + " <input type='number' name='Age'> " + 
 
    "Qualify:" + " <input type='text' name='qualification' id='qual'> " + 
 
    "Specilztn:" + " <input type='text' name='specialization' id='spec'> " + 
 
    "Percentage:" + " <input type='number' name='percentage' id='perc'> " + 
 
    " <input type='submit' id='subtn2'>"; 
 

 
     document.getElementById(dynamicInput).appendChild(newdiv); 
 
     counter++; 
 
     document.getElementById("removerow").style.display = "block"; 
 
    } 
 
} 
 

 

 
function removeInput() { 
 
    var x = document.getElementById("dynamicInput"); 
 
    if (x.hasChildNodes()) { 
 
     x.removeChild(x.childNodes[14]); 
 
    } 
 
} 
 

 
function formvalidation() { 
 

 
    var a = document.getElementById("user"); 
 
    var b = document.getElementById("age"); 
 
    var c = document.getElementById("qual"); 
 
    var d = document.getElementById("spec"); 
 
    var e = document.getElementById("perc"); 
 

 
    if (uservalidation()) { 
 
     if (agevalidation()) { 
 
      if (qualvalidation()) { 
 
       if (specvalidation()) { 
 
        if (percvalidation()) { 
 

 
        } 
 
        else { 
 
         return false; 
 
        } 
 
       } 
 
       else { 
 
        return false; 
 
       } 
 
      } 
 
      else { 
 
       return false; 
 
      } 
 
     } 
 
     else { 
 
      return false; 
 
     } 
 
    } 
 
    else { 
 
     return false; 
 
    } 
 

 
    function uservalidation() { 
 
     if (a.value == "") { 
 
      document.getElementById("uerr").innerHTML = "Enter UserName"; 
 
      document.getElementById("user").focus(); 
 
      return false; 
 
     } 
 
     else { 
 
      document.getElementById("uerr").style.visibility = "hidden"; 
 
      return true; 
 
     } 
 
    } 
 
    function agevalidation() { 
 
     if (b.value == "") { 
 
      document.getElementById("agerr").innerHTML = "Enter Age"; 
 
      document.getElementById("age").focus(); 
 
      return false; 
 
     } 
 
     else { 
 
      document.getElementById("agerr").style.visibility = "hidden"; 
 
      return true; 
 
     } 
 
    } 
 

 
    function qualvalidation() { 
 
     if (c.value == "") { 
 
      document.getElementById("qerr").innerHTML = "Enter Qualification"; 
 
      document.getElementById("qual").focus(); 
 
      return false; 
 
     } 
 
     else { 
 
      document.getElementById("qerr").style.visibility = "hidden"; 
 
      return true; 
 
     } 
 
    } 
 

 
    function specvalidation() { 
 
     if (d.value == "") { 
 
      document.getElementById("sperr").innerHTML = "Enter Specialization"; 
 
      document.getElementById("spec").focus(); 
 
      return false; 
 
     } 
 
     else { 
 
      document.getElementById("sperr").style.visibility = "hidden"; 
 
      return true; 
 
     } 
 
    } 
 

 
    function percvalidation() { 
 
     var addrowbtn = document.getElementById("addrow"); 
 
     if (e.value == "") { 
 
      document.getElementById("pererr").innerHTML = "Enter Percentage"; 
 
      document.getElementById("perc").focus(); 
 

 
      return false; 
 

 
     } 
 
     else { 
 
      document.getElementById("pererr").style.visibility = "hidden"; 
 

 
      addrowbtn.style.display = "block"; 
 

 
     } 
 
    } 
 
}
b{ 
 
    color:red; 
 
} 
 
#form2{ 
 
    display:none; 
 
} 
 
#addrow{ 
 
    display:none; 
 
    /* float:right; 
 
     margin-top:20px; 
 
     margin-right:45px; */ 
 
    position: absolute; 
 
    top: 8px; 
 
    right: 75px; 
 
    background:green; 
 
    color:white; 
 
    cursor:pointer; 
 
} 
 
#removerow{ 
 
    display:none; 
 
    background:red; 
 
    color:white; 
 
    cursor:pointer; 
 
    position: absolute; 
 
    top: 8px; 
 
    right: 5px; 
 
}
<form method="post" id="form1" name="form1" onsubmit="return formvalidation()"> 
 
    <div id="dynamicInput"> 
 
     User: <input type="text" name="user" id="user"> 
 
     Age: <input type="number" name="age" id="age"> 
 
     Qualify: <input type="text" name="qualification" id="qual"> 
 
     Specilztn:<input type="text" name="specialization" id="spec"> 
 
     Percentage:<input type="number" name="percentage" id="perc"> 
 
     <input type="submit" id="sbtn"> 
 

 

 
     <p> 
 
      <b id="uerr"> </b> 
 
      <b id="agerr"> </b> 
 
      <b id="qerr"> </b> 
 
      <b id="sperr"> </b> 
 
      <b id="pererr"> </b> 
 
     </p> 
 

 
    </div> 
 
    <input type="button" value="Add Row" id="addrow" onClick="addInput('dynamicInput');"> 
 
    <input type="button" value="Del Row" id="removerow" onClick="removeInput();"> 
 
</form>

+0

をタイトル –

答えて

0

ための検証を必要とします。いくつかのヒントがあります:

1)新しいdivを作成しているときに、IDを与えていないので、検証ではそれを見つけることができません。あなたの新しいdiv要素のいずれかの検証がないので、あなたの検証の

var newdiv = document.createElement('div'); 
newdiv.setAttribute("id", "Div1"); // you can generate the id from your counter 

2)すべてはそれを見つけるために何のロジックはありませんので、今はただそれにIDを与えたにもかかわらず、静的です。あなたの検証では、新しいdivについて何も知らない。

3)検証のもう1つの点は、複雑である必要はありません。あまりにも多くのことをする内部関数が多すぎます。少なくとも、少しきれいになり、あなたのformvalidation機能であなたの検証ごとにJUSTこれを行う:

var user = document.getElementById("user"); 
if(!user){ 
     document.getElementById("uerr").innerHTML = "Enter UserName"; 
     document.getElementById("user").focus(); 
     return false; 
} else { 
     document.getElementById("uerr").style.visibility = "hidden"; 
} 
+0

こんにちはマイケルからタグを削除し、私は(DIV1)場合は内部の検証ロジックを記述する方法を理解することができません{......} – GNANI

+0

あなたの既存の検証と似ていますが、あなたの動的IDと新しい動的検証タグまたはそのようなもので表示されます。私はあなたがちょうど 'div1.focus();新しい妥当性チェックタグを作成したくない場合は、falseを返します。 また、div1は単なる例です。動的に作成された要素をループし、新しいIDを使用します。 –

+0

また、どのアプリケーションを書いているのかよく分かりませんが、実際に検証機能を実行するときは、falseを返したかどうかを確認します。 –