/    Sign up×
Community /Pin to ProfileBookmark

Problem with validation…

This works:
if((form.abc.checked == true) && (form.abcaccess.value == “”)) {
alert(“nError!rnblah, blah”)
form.abcaccess.focus();
return false;
}

But this doesn’t:
if((form.name.checked == true) && (form.lname.value != true || form.middle.value != true || form.phone.value != true)) {
alert(“nError!rnblah, blah, blah.”)
form.name.focus();
return false;
}

Anytime I get past trying to validate more than two fields, it breaks.
What I want is to say if box 1 is checked, and box 2, 3 or 4 are not checked, error.

Thanks…

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanSep 29.2004 — Are the other items (Besides the first one) textboxes or textbased elements?

If not try:

<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--
if ((form.name.checked == true) &amp;&amp; (!form.lname.checked || !form.middle.value || !form.phone.value)){
window.alert("nError!rnblah, blah, blah.");
form.name.focus();
return false;
}
//--&gt;
&lt;/script&gt;



If not, can you please exaplin exactly what you want to do?

RyanJ
Copy linkTweet thisAlerts:
@russellSep 29.2004 — <i>
</i>function validate(f) {
// where f is the form, passed in
if(f.check1.checked==true) {
// make sure either check 2,3, or 4 is also checked
if(f.check2.checked==false &amp;&amp; f.check3.checked==false &amp;&amp; f.check4.checked==false) {
alert("You must check box 2, 3, or 4 if 1 is checked");
return false;
}
return true;
}
Copy linkTweet thisAlerts:
@Warren86Sep 29.2004 — 713:

Try this:

<HTML>

<Head>

<Script Language=JavaScript>

function validate(isForm){

count = 0;
nElements = isForm.length;
if (isForm.boxName[0].checked == true)
{
for (n=0; n<nElements; n++)
{
if (isForm[n].type = 'checkbox')
{
if (isForm.boxName[n].checked == true){count++}
}
}
}
else {alert('Box 1 was not checked')}
if (count == 1){alert('Only the first box was checked')}
if (count > 1){alert('Okay')}
count = 0;
}


</Script>

</Head>

<Body>

<form name="boxGroup">

<input type="checkbox" name="boxName" value=1>First<BR>

<input type="checkbox" name="boxName" value=2>Second<BR>

<input type="checkbox" name="boxName" value=3>Third<BR>

<input type="checkbox" name="boxName" value=4>Fourth<BR>

</form>

<input type=button value="Continue" onclick="validate(boxGroup)">

</Body>

</HTML>
Copy linkTweet thisAlerts:
@CharlesSep 29.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">

<meta name="Content-Script-Type" content="text/javascript">

<meta name="Content-Style-Type" content="text/css">

<title>Example</title>

<style type="text/css">

<!--

form {width:10em}

fieldset {padding:1ex}

label {display:block; margin:10}

button {display:block; margin:auto}

-->

</style>

<script type="text/javascript">

<!--

function check (f) {if (f.giantSays.checked && !(/S/.test(f.fee.value) || /S/.test(f.fie.value) || /S/.test(f.foe.value) || /S/.test(f.fum.value))) {alert ('Please specify a Fee, Fie, Foe or Fum.'); return false}}

// -->

</script>

</head>

<body>

<form action="someScript.pl" onsubmit="return check(this)">

<fieldset>

<legend>Example</legend>

<label><input name="giantSays" type="checkbox">Giant Says...</label>

<label>Fee<input name="fee"></label>

<label>Fie<input name="fie" type="text"></label>

<label>Foe<input name="foe" type="text"></label>

<label>Fum<input name="fum" type="text"></label>

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

</fieldset>

</form>

</body>

</html>[/font]
×

Success!

Help @713dad 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,
)...