/    Sign up×
Community /Pin to ProfileBookmark

form validation: select and checkbox

Hi,
I’m looking for a script that:
1) verifies that a select option has been chosen
2) at least 1 checkbox has been ticked

My checkboxes are generated dinamically with PHP, so the number varies…

Can anyone help?

Thanks and bye

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroJul 25.2004 — Here ya go. It should handle any number of checkboxes and selects, since it just goes through all elements in a form and looks at the <select> and <input type="checkbox"> tags. The parameter "elem" is the id of the form you want it to validate.
<i>
</i>function validate(elem) {
src=document.getElementById(elem);
var checked=false;
var selected=false;
var i=0;
for(i=0; i&lt;src.elements.length; i++) {
if(src.elements[i].type.toLowerCase()=="checkbox" &amp;&amp; src.elements[i].checked) {
checked=true;
}
if(src.elements[i].tagName.toLowerCase()=="select" &amp;&amp; src.elements[i].selectedIndex!=0) {
selected=true;
}
}
return (checked&amp;&amp;selected);
}
Copy linkTweet thisAlerts:
@CharlesJul 26.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Example</title>

<style type="text/css">

<!--

form {width:10em}

fieldset {padding:1em; text-align:right}

label {display:block;}

form div {clear:both; text-align:center}

-->

</style>

<script type="text/javascript">

<!--

function verify (f) {

if (f.giantSays.selectedIndex == 0) {alert ('What did the giant say?'); f.giantSays.focus(); return false}

var i,e

var checked = false

for (i=0; e=f.elements[i]; i++) {if (e.checked) checked = true}

if (!checked) {alert('Which Stooge?'); return false}

}

// -->

</script>



</head>

<body>

<form action="senMe.pl" onsubmit="return verify(this)">

<fieldset>

<legend>Giant Says</legend>

<select id="giantSays">

<option>---</option>

<option>Fee</option>

<option>Fie</option>

<option>Foe</option>

<option>Fum</option>

</select>

</fieldset>

<fieldset>

<legend>Stooge</legend>

<label>Mo<input type="checkbox"></label>

<label>Larry<input type="checkbox"></label>

<label>Curley<input type="checkbox"></label>

</fieldset>

<div><button type="submit">Submit</button></div>

</form>

</body>

</html>



[/font]
×

Success!

Help @lucesei 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.18,
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,
)...