• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

validating dynamic form objects

i have form objects listed with the following for loop but the javascript doesnt validate if 2 items are listed.
pls help
<form name="form1" method="post" action="done.php">
  <table width="388"  border="0" cellspacing="5" cellpadding="0">
    <tr>
      <td><?php
for ($i=1; $i<=$idv; $i++)
{
?></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="115">Item Category </td>
      <td width="258"><select name="itemcat<? echo($i);?>">
         <option selected value="">Select One</option>
        <option value="Celtel100">Celtel (100)</option>
                                <option value="celtel200">Celtel (200)</option>
                                <option value="celtel300">Celtel (300)</option>
                                <option value="celtel400">Celtel (400)</option>
                                <option value="celtel500">Celtel (500)</option>                              
      </select>
      <input name="datecreated<? echo($i)?>" type="hidden" id="datecreated" value="<? echo(date("Y-m-d"))?>">
      <input name="idv" type="hidden" id="idv" value="<? echo($idv);?>"></td>
    </tr>
    <tr>
      <td>Serial Number </td>
      <td><input name="S_no<? echo($i)?>" type="text" size="40" maxlength="40"></td>
    </tr>
    <tr>
      <td>Pin Number </td>
      <td><input name="P_no<? echo($i)?>" type="text" size="40" maxlength="40"></td>
    </tr>
    <tr>
      <td>Expiry Date </td>
      <td><input name="e_date<? echo($i)?>" type="text" id="e_date<? echo($i)?>">
        (yy-mm-dd)</td>
    </tr>
    <tr>
      <td><?php  }?></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input type="submit" name="Submit" value="Add Item(s)" onClick = "return frmValidation(this)"></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>


function frmValidation(obj){
      var frm = obj.form;
        var idvv = document.form1.idv.value;
      var val = frm.idv.value;
        //document.write(idvv);
   for (var x = 1; x <= idvv; x++)
   {
   var vas = 'document.form1.itemcat'+x+'.value';
   var varw = 'document.form1.itemcat'+x;
   //datecreated
   var datecreated = 'document.form1.datecreated'+x+'.value';
   var datecreatedw = 'document.form1.datecreated'+x;
   
   var S_no = 'document.form1.S_no'+x+'.value';
   var S_now = 'document.form1.S_no'+x;
   
   var P_no = 'document.form1.P_no'+x+'.value';
   var P_now = 'document.form1.P_no'+x;
   
   var e_date = 'document.form1.e_date'+x+'.value';
   var e_datew = 'document.form1.e_date'+x;
   
   var vas = eval(vas);
    var varw = eval(varw);
      
       var datecreated = eval(datecreated);
    var datecreatedw = eval(datecreatedw);
      
       var S_no = eval(S_no);
    var S_now = eval(S_now);
      
       var P_no = eval(P_no);
    var P_now = eval(P_now);
      
       var e_date = eval(e_date);
    var e_datew = eval(e_datew);
   if(vas=="")
        {
        alert("Please enter Item Category.");
        varw.focus();
            //document.form1.name.value= "";
        return false;
        }

            if(S_no=="")
        {
        alert("Please enter Serial Number.");
        S_now.focus();
            S_now.value= "";
        return false;
        }
            if(P_no=="")
        {
        alert("Please enter Pin Number.");
        P_now.focus();
            P_now.value= "";
        return false;
        }
            if(e_date=="")
        {
        alert("Please enter Pin Number.");
        e_datew.focus();
            e_datew.value= "";
        return false;
        }
    }
0
woleraymond
Asked:
woleraymond
  • 3
1 Solution
 
Michel PlungjanIT ExpertCommented:
put       <input name="idv" type="hidden" id="idv" value="<? echo($idv);?>"></td>

outside the loop
0
 
Michel PlungjanIT ExpertCommented:
and you are missing a } at the end
And you are missing  id="datecreated<? echo($i)?>"
at <? echo($i)?>
0
 
Michel PlungjanIT ExpertCommented:
and you need to change the last
        alert("Please enter Pin Number.");

to

        alert("Please enter Expiry date.");
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now