/    Sign up×
Community /Pin to ProfileBookmark

Handling Form elements like name="product2[]"

When you use the same name with a [] for multiple fields (hidden and text), the result is concatenated. Because of the shopping cart I use (mals-e.com), I have to use this technique.

But I’m having difficulting doing some form validation on those product2[] elements (see code below).

I’d like to have a “onsubmit” javascript event handler
tied to the form that could verify that if the user has
clicked on the checkbox (qty2), then he MUST have entered
an account number in product2[] (the 4th on that is!).

I don’t do javascript and this is only giving me a headache
in trying to figure out how to reference this particular
DOM form object in the verification function.
Any javascript code example would be greatly appreciated!

…. stuff deleted ….

<input type=”hidden” name=”product2[]” value=”Use my Shipping (10% off)”>
<input type=”checkbox” name=”qty2″ value=”1″>
<input type=”hidden” name=”price2″ value=”-4.80″>

<select size=”1″ name=”product2[]”
<option value=”Fedex”>Fedex</option>
<option value=”UPS”>UPS</option>
<option value=”DHL”>DHL</option>
<option value=”Airborne”>Airborne</option>
</select>

<i>account#</i>
<input type=”hidden” name=”product2[]” value=”acct #”>
<input type=”text” name=”product2[]” size=”20″ >

…. stuff deleted ….

Cheers!
– Christian

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@xataku_nakusuteJul 29.2003 — perhaps it is becuz you have two fields....(<input> and <select>) assigned to the same name(product2[])
Copy linkTweet thisAlerts:
@scoobauthorJul 29.2003 — [i]Originally posted by xataku_nakusute [/i]

[B]perhaps it is becuz you have two fields....(<input> and <select>) assigned to the same name(product2[]) [/B][/QUOTE]


Actually, if you look closely, you will see that there are [B]4 fields[/B] with the same name ([I]product2[][/I] ). I know it's strange but it's legit html form stuff. It is interpreted by my shopping cart as concatenating each of those elements onto one product desctiption line.

So, the issue I have is: it's legit html form code but I have no idea how to access the [B]4th[/B] instance of these elements (the one where the user enters an account number.

  • - Scoob
  • Copy linkTweet thisAlerts:
    @AdamGundryJul 29.2003 — You should be able to use [b]formname.elements[#][/b] where # is the number of the form element (in source code order). See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/form.html#1193375 for more details.

    Adam
    Copy linkTweet thisAlerts:
    @xataku_nakusuteJul 29.2003 — sorry, i mustve overlooked the "hidden" attribute in the tag.....

    perhaps, you may assign an id to the field....

    maybe:

    <script type="text/javascript">

    checker = document.f1.i1.disabled;

    function tos()

    {

    document.f1.i1.disabled=false;

    }

    function dis()

    {

    document.f1.i1.disabled=true;

    }

    function check(checker)

    {

    if(document.f1.i1.disabled==1)

    {

    tos();

    }

    else

    {

    dis();

    }

    }

    </script>

    and with your checkbox....

    onclick="check()"

    uhhh....im not sure if thats what youre asking for...im just a bit confused with what youre trying to ask...

    the script i provided above will simply disable your submit button(with the name of i1) in your form(with the name of f1)
    Copy linkTweet thisAlerts:
    @scoobauthorJul 29.2003 — Thank you AdamGundry for the

    rapid info that lead to the perfect solution!

    Solution:

    function validateEntries (form) {

    var FedexAccountEleIdx = 9;

    with(form) {

    // First, if user checked Fedex checkbox he must also enter account #
    if (qty2.checked) {
    if (form.elements[FedexAccountEleIdx].value.length < 3) {
    alert("You must enter a valid Shipping account number!");
    return(false);
    }
    }
    }
    }
    ×

    Success!

    Help @scoob spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...