steveclondon Posted December 16, 2008 Share Posted December 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 16, 2008 Share Posted December 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.