title: How to validate data in dynamically created table rows that are input fields
body: <p>I have a dynamically created table where row2 and column2 onward elements are input fields. User can change these fields and submit.</p>
  
  <p>I want to put a check so that in any row:</p>
  
  <ul>
  <li>The 1st input field should not be greater than 2nd, 3rd or 4th </li>
  <li>The 2nd input field should not be greater than 3rd or 4th. And it should not be smaller than 1st.</li>
  <li>The 3rd input field should not be greater than 4th. And it should not be smaller than 1st or 2nd.</li>
  <li>The 4th input field should not be smaller than 1st or 2nd or 3rd.</li>
  </ul>
  
  <p>So if the user tries to change any value to an invalid value and click submit button, then an error message should be displayed and form should not be submitted. So only option on the message window should be an OK button which should bring the user back to the old page (before changes were made to any field).</p>
  
  <p>I tried doing it by making use of getElementById() and giving same id for all four input fields in a row, but the getElementById() returns the first element if more than one id exists (id not being unique).</p>
  
  <p>I checked possibility to try getElementsByName() but the value of name attribute  is being generated dynamically and only "_good_high", "_warning_low", "_warning_high" and "_critical_low" is constant part of it.</p>
  
  <p>Can you please suggest me some way to add this validation?</p>
  
  <p>Link to jsfiddle is <a href="https://jsfiddle.net/fiddle300/e75evmxo/2/" rel="nofollow">ExampleFiddle</a></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;CommDesk AdminPage&lt;/title&gt;
  &lt;meta HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1"&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  addPlusSign();
  $(".btn1").click(function(){
  $(".expand1").toggle();
  var btn1Text = $(".btn1").text();
  if(btn1Text.indexOf("+") &gt; -1){
  var temp = btn1Text.replace(/\+|\-/ig, '-');
  $(".btn1").text(temp);
  } else if (btn1Text.indexOf("-") &gt; -1){
  var temp = btn1Text.replace(/\+|\-/ig, '+');
  $(".btn1").text(temp);
  }
  });
  })
  function addPlusSign(){
  if($(".expand1")){
  var btn1Text = $(".btn1").text();
  $(".btn1").text(btn1Text + " [+]");
  }
  }
  $(function () {
  $('.admin-form')
  //we need to save values from all inputs with class 'admin-input'
  .find(':input.admin-input')
  .each(function () {
  //save old value in each input's data cache
  $(this).data('oldValue', $(this).val())
  })
  .end()
  .submit(function (ev) {
  var changed = false;
  $(':input.admin-input', this).filter(function () {
  if($(this).val() != $(this).data('oldValue')){
  changed = true;
  }
  });
  if($(this).hasClass('changed') &amp;&amp; (!changed)){
  alert("None of the thresholds were changed!");
  ev.preventDefault()
  }
  if($(this).hasClass('changed') &amp;&amp; changed){
  var allowSubmit = window.confirm("You have set a unique threshold for one or more sub-elements below. Are you sure you want to reset them all?")
  if (!allowSubmit)
  ev.preventDefault()
  }
  });
  });
  $(document).on('input', '.admin-input', function(){
  $(this).closest('form').addClass('changed');
  });
  &lt;/script&gt;
  
  &lt;style&gt;
  .expand1 { display: none;
  }
  .btn1 { cursor: pointer;
  }
  body {
  background-color: rgb(255,255,255);
  font: 15px Verdana, Helvetica, sans-serif;
  }
  span.note1 {float:left}
  span.note2 {font-size:90%}
  table#t02, #t02 th, #t02 td {
  border: none;
  border-collapse: collapse;
  font-size:90%;
  font-weight:normal;
  }
  
  #button1{
  position: relative;
  top:10px;
  left:75%;
  color: white;
  background-color: rgb(0,89,132);
  font-weight: bold;
  }
  &lt;/style&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" method="post" class="admin-form"&gt;
  &lt;div style="float:left; width:50%"&gt;
  &lt;table id="t02" class="table2"&gt;
  &lt;tr&gt;
  &lt;th style="padding:0 30px 0 0;"&gt;&lt;/th&gt;
  &lt;th&gt;&lt;/th&gt;
  &lt;th style="padding:0 10px 0 0;"&gt;Green&lt;/th&gt;
  &lt;th colspan="3" style="padding:0 10px 0 0"&gt;Yellow&lt;/th&gt;
  &lt;th&gt;&lt;/th&gt;
  &lt;th style="padding:0 10px 0 0"&gt;Red&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td class="btn1" style="padding:0 30px 0 0;"&gt;&lt;b&gt;Row1&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;&amp;lt&lt;/td&gt;
  &lt;td style="padding:0 10px 0 0"&gt;&lt;input type="text", class="admin-input", name="acd_call_volume_good_high", value="50", id="1", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td&gt;&lt;input type="text", class="admin-input", name="acd_call_volume_warning_low", value="50", id="1", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td&gt;to&lt;/td&gt;
  &lt;td style="padding:0 10px 0 0"&gt;&lt;input type="text", class="admin-input", name="acd_call_volume_warning_high", value="100", id="1", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td&gt;&amp;gt&lt;/td&gt;
  &lt;td style="padding:0 10px 0 0"&gt;&lt;input type="text", class="admin-input", name="acd_call_volume_critical_low", value="100", id="1", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td align="center" class="expand1"&gt;SubRow1&lt;/td&gt;
  &lt;td class="expand1"&gt;&amp;lt&lt;/td&gt;
  &lt;td class="expand1"&gt;&lt;input type="text", name="acd_call_volume_good_high_SubRow1", value="50", id="2", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td class="expand1"&gt;&lt;input type="text", name="acd_call_volume_warning_low_SubRow1", value="50", id="2", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td class="expand1"&gt;to&lt;/td&gt;
  &lt;td class="expand1"&gt;&lt;input type="text", name="acd_call_volume_warning_high_SubRow1", value="100", id="2", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;td class="expand1"&gt;&amp;gt&lt;/td&gt;
  &lt;td class="expand1"&gt;&lt;input type="text", name="acd_call_volume_critical_low_SubRow1", value="100", id="2", size="3", maxlength="3"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;input type="submit" name="submitButton" value="Submit" id="button1" style="height:50px; width:100px"/&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>