/    Sign up×
Community /Pin to ProfileBookmark

Validating a form that contains array notation labels

I’m wanting to do a form validation before it is submitted, and I am having a problem inside my JS checkform() function getting a hold of the “product[]” values, which in the form are in array notation.

For some reason, I am getting JS errors such as “product has no properties” etc. when I try to use “product” or “product[]” in various ways.

I’m not too experienced in JS. Things like getElementById(‘product[]’).selectedIndex are not working. I did a big search on the web, but found nothing that worked.

What I want to do ultimately is get the validation function to return false (i.e. a bad form submission) if what in the drop-down select box the user has selected is the first option.

Can anyone assist? Thanks in advance.

function checkform
{
if ( ??? )
{
alert(“Please select an option”);
return true;
}
else
{
return false;
}
}

<FORM ACTION=”http://ww4.aitsafe.com/cf/add.cfm” METHOD=”post” target=”cart” onSubmit=”return checkform()”>
<INPUT TYPE=”hidden” NAME=”userid” VALUE=”6887906″>
<INPUT TYPE=”hidden” NAME=”product[]” VALUE=”BLACKTSHIRT”>
<INPUT TYPE=”hidden” NAME=”price” VALUE=”22.00″>
<br>
<br>
<font size=”2″>Size/font>
<select name=”product[]” id=”product[]”>
<option selected>Select</option>
<option>M</option>
<option>L</option>
<option>XL</option>
</select>
<p>
<input name=”imageField22″ type=”image” src=”images/add_to_cart.gif” width=”70″ height=”30″ border=”0″>
</p>
</FORM>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 18.2008 — [B]id[/B] must be unique on document/session, thus in your case you [B][I]must not [/I][/B]use the array notation for the ids, but [I]only[/I] for names.

Now use [B]getElementsByName()[/B] method. Note that getElementsByName() returns a collection of elements, not a single one. If there is only a single elements with that name, it will have the 0 index in the collection: document.getElementsByName('name[]')[B][0][/B]
Copy linkTweet thisAlerts:
@miner2049erauthorJan 20.2008 — Thank you - that did it!
×

Success!

Help @miner2049er 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.24,
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,
)...