/    Sign up×
Community /Pin to ProfileBookmark

Validating selection lists

Hey,
I have a selection list with 9 options in it. I am allowing the user to select multiple options but I need to check that they have selected exactly 5 options from the list.

I have tried the seletedIndex but I believe this is not very useful when working with the Multiple attribute.

Please help

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@oleragFeb 04.2004 — [code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Sample</title>
<script type="text/javascript">
<!--
function execute(form) {
var count=0;
for(var i=0;i<form.selectList1.length;i++) {
if (form.selectList1.options[i].selected) {
count++;
}
}

if (count == 5) {
alert("OK - Five options selected");
}
else {
alert("Failed - Exactly five options must be selected.");
}
}
// -->
</script>

<div style="text-align: center;">
<strong>Sample</strong>
</div>

<form action="">
<p>
<select name="selectList1" size="5" multiple>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
<option value="7">Option 7</option>
<option value="8">Option 8</option>
</select>
</p>
<p style="text-align: center;">
<input type="button" name="btn1" value="Execute" onClick="execute(this.form)">
</p>
</form>

[/code]
Copy linkTweet thisAlerts:
@kristie_annauthorFeb 04.2004 — Thank you so much - problem solved ?
Copy linkTweet thisAlerts:
@VladdyFeb 04.2004 — [i]Originally posted by kristie ann [/i]

[B]Thank you so much - problem solved ? [/B][/QUOTE]

Not really. Any client-side "validation" can be easily circumvented. Real validation should be done server side code. Client side validation should only be done for user convinience (they are spared a trip to the server when a field is missing). Anything that is of a security/application performance concern, should be left to server side.

Also, if you do do the client side verification, it should be done using form onsubmit event. Submit button onclick even is not fired when a user presses "Enter"...
×

Success!

Help @kristie_ann 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...