Hi guys,

I'm having a page called dependent list whereby HR department could update the employee's dependent details. The page contain php generated input fields in a form.
(whereby PHP will search the array of dependent list in the database then display it within HTML input field value and each field separated using unique increased variable i value at the end of each ID of input field, example: id=dependent_fullname_1)

I have a javascript function that could validate these field with unique ID (example: id=dependent_fullname_1) and the numbers at the end of every ID will be increase according to dependent sequence.

Problem: Let's say PHP has found total 6 dependents for this employee, this javascript validation function only detect 5 dependent and assume the 6th field is not exist hence return the validation not functioning. If I hardcoded the maximum value of i to 5 dependent into for loops [ for (var i=0; i <= 5; i++)] then only the validation works.

Here's the code, what i'm doing wrong?

Code JavaScript:
function validate_updateForm()
{
 
var ttl_dpnt = document.getElementById('total_dependent').value;      ////Grab total dependent
var real_val = ttl_dpnt-1;  // Last value of variable i by PHP generated is  i++ at the end of PGP while loop, the value of variable i always having extra 1
 
    for (var i=0; i <= real_val; i++)
    {
		if(i==0){
		i = 1;
		}
 
		var dependent_name = document.getElementById('dependent_fullname_' + i).value;
		var dependent_dob = document.getElementById('dependent_dob_' + i).value;
		var dependent_ic = document.getElementById('dependent_ic_' + i).value;
 
		if( (dependent_name == '') || (dependent_dob == '') || (dependent_ic == '') ) {
		alert('<?php echo LABEL_EMPTY_FIELD_FOUND_ERROR;?>');
		return false;	
 
    }
 
 
else
 {
var agree=confirm('<?php echo LABEL_CONFIRM_CONTINUE;?>');
if (agree)
	return true ;
else
	return false ;
    }

My generated PHP&HTML code are

Code HTML4Strict:
$i=1;
while(statement){
<input type="text" name="dependent_fullname_<? echo $i;?>" id="dependent_fullname_<? echo $i;?>" value="<? echo $dependent_fullname;?>" placeholder="<? echo LABEL_NAME;?>" /> <input type="hidden" id="dependent_id_<? echo $i;?>" name="dependent_id_<? echo $i;?>" value="<? echo $dependent_id;?>" />
    </td>
    <td align="center" width="201" class="Labelling"><input name="dependent_dob_<? echo $i;?>" type="text" id="dependent_dob_<? echo $i;?>" value="<? echo $dependent_dob;?>"/>
    <br />
    (dd/mm/yyyy)</td>
    <td align="center" width="214" class="Labelling">
      <label>
        <input type="text" name="dependent_ic_<? echo $i;?>" id="dependent_ic_<? echo $i;?>" value="<? echo $dependent_ic;?>" placeholder="<? echo LABEL_IC_NUMBER;?>" onblur='checkNum(this)' />
      </label>
}
$i++;