/    Sign up×
Community /Pin to ProfileBookmark

checkbox validation with form validation

I have one of these scripts:
——begin existing scripts—
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! [url]http://javascript.internet.com[/url] –>

<!– Begin
function checkFields() {
missinginfo = “”;
if (document.form.url.value == “”) {
missinginfo += “n – URL”;
}
if(document.form.title.value == “”) {
missinginfo += “n – Title”;
}
if(document.form.description.value == “”) {
missinginfo += “n – Description”;
}
if ((document.form.email.value == “”) ||
(document.form.email.value.indexOf(‘@’) == -1) ||
(document.form.email.value.indexOf(‘.’) == -1)) {
missinginfo += “n – Email address”;
}

if (missinginfo != “”) {
missinginfo =”_____________________________n” +
“You failed to correctly fill in your:n” +
missinginfo + “n_
__
__________________________” +
“nAll fields need to be entered correctly nbefore message can be sent!”;
alert(missinginfo);
return false;
}
else return true;
}
// End –>
</script>
—end of existing scvript——–
Now i would like to add 2 or 3 unchecked checkboxes to my form and the user must select at least one, otherwise the form will not be sent.
I have tried a few things already but nothing so far worked.
Can anyone please help out???

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Phil_KarrasJun 28.2004 — // Catches if neither check box is checked

if(!document.form.CheckBox1.checked &&

!document.form.CheckBox2.checked) {

// your error stuff goes here

}

// Catches if both check boxes are checked

if(document.form.CheckBox1.checked &&

document.form.CheckBox2.checked) {

// your error stuff goes here

}
Copy linkTweet thisAlerts:
@BhanuJun 28.2004 — you can do this way

Bhanu
-----------------------------------------


<html>

<head>

<title>bhanu</title>

</head>

<script language="JavaScript">

function checkbox(){

if(document.all['one'].checked == true||document.all['two'].checked == true||document.all['thr'].checked == true){alert('checked');}

}

</script>

<body >

<form id="fm" onsubmit="checkbox()">

<div id="D">This is div text</div>

<input type="Checkbox" id="one" value="1">one<br>

<input type="Checkbox" id="two" value="2">two<br>

<input type="Checkbox" id="thr" value="3">three<br>

<input type="submit">

</form>

</body>

</html>
-------------------------------------------

Copy linkTweet thisAlerts:
@humpty_dumptyauthorJun 28.2004 — <script language="JavaScript">

function CountChars(whichform) {

var mystr;

var mystr2;

var mystr3;

if (whichform.additional_info) {

mystr3 = whichform.additional_info.value;

} else {

mystr3 = '';

}

if ((mystr.length > 250) || (mystr3.length > 250)){

alert("We're sorry, but you are only allowed to enter a maximum of 250 characters.");

return(0);

}

return(1);

}

</script>

<script language="JavaScript">

function checkFields() {

emptyfields = "";

if (document.form.url.value == "") {

emptyfields += "n - URL";

}

if (document.form.title.value == "") {

emptyfields += "n - Title";

}

if(document.form.description.value == "") {

emptyfields += "n - Description";

}

if ((document.form.email.value == "") ||

(document.form.email.value.indexOf('@') == -1) ||

(document.form.email.value.indexOf('.') == -1)) {

emptyfields += "n - Email address";

}

var clickedButton = "";

for(i=0;i<2;i++){

if(document.form.payment[ i ].checked == true){

clickedButton = "true";

break

}

}

if (clickedButton == "") {

emptyfields += "n - Payment Selection";

}

if (emptyfields!= "") {

emptyfields = "These fields are required:n" +

emptyfields + "nnPlease fill in all required feilds";

alert(emptyfields);

return false;

}

else return true;

}

</script>
----------------------------------


worked eventually for me, this really does exactly what i want and works in both main browsers right.

Thanks for your quick replies
Copy linkTweet thisAlerts:
@humpty_dumptyauthorJul 01.2004 — Now i run into another problem...

With the form validation script i just postet here (that has the "emptyfields" thing) i want to add a group of 3 checkboxes from which either or all can be checked.

However all my day went past me on trying different variations.

I got as far as making something work - that validated each of them seperatly, meaning only all had to be checked before it could be sent, makes no sense.

I can use radios, but then i have only 1 choice to select, i got that to work, but again i would like to offer any or all options to be ticked.

Is it perhaps not possible because of the "emptyfields" structure of the script, or is there a way to integrate other script part that do validate multiple checkboxes???

If so, how and which code???
Copy linkTweet thisAlerts:
@Phil_KarrasJul 01.2004 — Of course there's a way.

You must think out clearly what you want to do (You've probably done this but your description does not make it clear enough for me.)

What is accceptable? A check box is after all either checked or not so there's no need to check their values, all you need to do, if I understand what you want, is to check that at least one of them is checked.

If which case you can use if statements or something else.
<i>
</i>// Check a group of four check boxes &amp; make sure
// at least one of them is checked.

var allOK = 0; // or false

if(doc.form.cb1.checked) {
allOK = 1; // or true
}
else if (doc.form.cb2.checked) {
allOK = 1; // or true
}
else if (doc.form.cb3.checked) {
allOK = 1; // or true
}
else if (doc.form.cb4.checked) {
allOK = 1; // or true
}

if(allOK == 0) { // or == false
alert("Oops, you need to check at least one box.");
}

where

[color=red]doc[/color] should be "document"

[color=red]form[/color] should be the name of your form, or it should be forms[0] if this is the first form on the page.

[color=red]cb#[/color] is the name of your check boxes
×

Success!

Help @humpty_dumpty 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.17,
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,
)...