Form Validation w/ dynamic fields - same name & id

Results 1 to 3 of 3

Thread: Form Validation w/ dynamic fields - same name & id

  1. #1
    Scott Vincent Guest

    Default Form Validation w/ dynamic fields - same name & id

    I have a dynamic .asp form which runs in three parts. <BR>It is a return items form. <BR><BR>Part 1 - asks for number of items to be returned. <BR><BR>Part 2 - creates a dynamic "set" of input boxes based on part 1. <BR>Each "set" includes 4 input boxes. .. (part num, serial num, invoice num, reason). So if a person wants to return 10 items there are 40 fields to fill out. Each "set" of input boxes has the same name (txtPartNum, txtSerialNum, txtInvoiceNum, txtReason). The name value is not incremented in any way. <BR><BR>This info is then sent to part 3 (CDONTS mailer). I use a looping forms collection to retrieve all the values. This works well. <BR><BR>What I need is a way to validate all the form elements. When I write a traditional javascript handler it works great if there is just 1 item to be returned. But if 2 or more "sets" of info are on the form it doesn&#039;t work. <BR><BR>I am thinking that perhaps I can use a "all" property somewhere to check the entire form but am not sure how to implement it. <BR><BR>Any other suggestions are most appreciated? <BR><BR>Thanks, <BR>Scott <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Form Validation w/ dynamic fields - same name

    A very fundamental concept of JavaScript and FORM fields: ANY TIME two fields have the same name, the JS engine turns them into an *array* of that name.<BR><BR>So if you have:<BR><BR>&#060;INPUT Type=Text Name=whatever ...&#062;<BR>...<BR>&#060;INPUT Type=Text Name=whatever ...&#062;<BR><BR>then you now have an *array* named "whatever". So you&#039;d need to do:<BR><BR>whatevers = document.formName.whatever<BR>for ( i = 0; i &#060; whatevers.length; ++i )<BR>{<BR>&nbsp; &nbsp; validate( whatevers[i] );<BR>}<BR><BR>BUT...<BR><BR>I think you are making a mistake using this technique. It will *not* work if you ever use checkboxes or radio buttons on your form! That&#039;s because an unchecked checkbox or a group of radio buttons with no selection at all send *nothing at all* to the next page. Also, you are relying on all browsers to always put your like-named fields into the same order in the posted data. I do *not* believe there is anything in the HTML spec that says they must do that.<BR><BR>I really think you would be better off appending a number to each field name and then processing the fields by the combo of name and number.<BR><BR>In JS code:<BR><BR>for ( i = 1; i &#060; maxNum; ++i )<BR>{<BR>&nbsp; &nbsp; elem = document.form.elements["whatever"+i]<BR>&nbsp; &nbsp; ...<BR>}<BR><BR>In the VBS code in ASP:<BR><BR>FOR i = 1 To MaxNum<BR>&nbsp; &nbsp; val = Request.Form("whatever"&i)<BR>&nbsp; &nbsp; ...<BR>Next<BR><BR>Opinion, only. But a fairly strong one.<BR><BR>

  3. #3
    Join Date
    Dec 1969

    Default RE: Form Validation w/ dynamic fields - same name

    Yes, that is what I was afraid of.<BR><BR>Originally I had programmed for the name to have an incremented value on the end; PartNum1, PartNum2, etc..., but I wasn&#039;t able to get the results to print out to the mailer (ASPMail).<BR>So I was stuck with using the forms collection route with same names.<BR><BR>Thanks for the tip.<BR>Scott

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts