Jump to content

Check dynamic generated form fields


steveclondon

Recommended Posts

HI,

 

I have a form that is dynamically generated. I have no problem with the form that works fine, but now I want to check if a user has filled out the information. A small piece of code is here when the form is made.

 

while (i<something)

{

heightInput.setAttribute("id","");

heightInput.id="itemHeight_"+i;

heightInput.setAttribute("name","");

heightInput.name="itemHeight_"+i;

i++;

}

so now I have names for my input fields of itemHeight_0, itemHeight_1 and so on.

 

I now want to check these when they are submitted, I have tried this below but of course it is not seeing itemName_+a; as the fieldname. I could pull all the elements by tag and then go through the array checking these, but I have other text boxes on the form that that would be difficult. Anyone got any ideas? I don't know how many of these text boxes will be generated so I need an automated approach.

 

a=0;

while(a<numberItems)

{

//the form has already been got by element id and stored as theForm.

var setName='itemName_'+a;

alert(setName);

 

 

if (theForm.setName.value=='')

{

alert('name not set');

hasError=true;

sError += " Please xxx.\n";

}

a++;

} // while

Edit/Delete Message

Link to comment
Share on other sites

Well, it would have been nice if you provided the code to actually create the fields. The above only shows where you are settng properties for the element.

 

But, in the code where you create the element you are setting the name as

heightInput.name="itemHeight_"+i;

 

But in the code to reference the field you are using

var setName='itemName_'+a;

 

'itemHeight_' != 'itemName_'

 

If that was just a typo for posting and they really are the same in the code, I would suggest trying to reference the fields via their ID.

 

a=0;
while(a<numberItems)
{
    //the form has already been got by element id and stored as theForm.
    var setID='itemName_'+a;
    alert(setID);

    if (document.getElementById(setID) && !document.getElementById(setID))
    {
        alert('name not set');
        hasError=true;
        sError += " Please xxx.\n";
    }
    a++;
} // while

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.