/    Sign up×
Community /Pin to ProfileBookmark

Reverse Validating Script

I have a great script that if a check box is checked then an associated drop down is required.

How would I reverse this saying, if I have a drop down option selected then the associated checkbox MUST be checked.

Thanks for the help. Here is my code:

<HTML>
<HEAD>
<TITLE></TITLE>

<script type=”text/javascript”>
function validateFunction2() {
var fo = document.formname;
if (!fo.CETS.checked && !fo.Appointment_Cards.checked) {
alert(“You must check at least one item to order.”);
return false;
}
return true;
}
</script>

<script type=”text/javascript”>
function validateFunction() {
var j = 1;
for (var i = 0; i < document.formname.elements.length; i++) {
var elem = document.formname.elements[i];
if (elem.type == “checkbox”) {

var chk_field_name = ‘field’+j;
var drop_field_name = ‘Item’+j;
if (document.getElementById(chk_field_name).checked && document.getElementById(drop_field_name).value == ”) {
alert(“You must select a Qty for “+drop_field_name+” from the drop down box.”);
return false;
}
j++;
}
}
return true;
}
</script>

<script language=”javascript” type=”text/javascript”>
var prods = new Array();
prods[0] = “8”;
prods[1] = “8”;
prods[2] = “8”;
prods[3] = “8”;
prods[4] = “8”;

function setPrice(selObj) {
var temp = document.getElementById(‘qqq’);
temp.value = prods[selObj.selectedIndex];
}
</script>

<script language=”javascript” type=”text/javascript”>
var prods1 = new Array();
prods1[0] = “9”;
prods1[1] = “9”;
prods1[2] = “9”;
prods1[3] = “9”;
prods1[4] = “9”;

function setPrice1(selObj) {
var temp1 = document.getElementById(‘AP’);
temp1.value = prods1[selObj.selectedIndex];
}
</script>

</HEAD>
<BODY BGCOLOR=#F4F4F4 leftmargin=”0″ topmargin=”0″ text=”#000000″ link=”#000000″ vlink=”#000000″ alink=”#000000″>

<center>

<form name=”formname” action=”ADTorder.pl” method=POST ENCTYPE=”multipart/form-data” onSubmit=”return

(validateFunction2(this) && validateFunction(this))”>

<table border=0 cellspacing=1 width=100% cellpadding=0 bgcolor=”dddddd”>
<tr>
<td>
<table border=0 cellspacing=2 width=100% cellpadding=3 bgcolor=”Ffffff”>
<tr>
<td width=”10%”><b>Select</td>
<td width=”30%”><b>Item</td>
<td width=”10%”><b>Qty</td>
<td width=”45%”><b>Options</td>
</tr>

<tr>
<td bgcolor=”FAFAFA”><input type=”checkbox” name=”CETS” id=”field1″ value=”CETS”></td>
<td bgcolor=”FAFAFA”>CETS</td>
<td bgcolor=”FAFAFA”>
<select name=”CETS_QTY” id=”Item1″ onChange=”setPrice(this)”>
<option value=””></option>
<option value=”500″>500</option>
<option value=”1000″>1000</option>
<option value=”1500″>1500</option>
<option value=”2000″>2000</option>
</select>
<input id=”qqq” type=”hidden” name=”productid2″ value=””>
</td>
<td nowrap bgcolor=”FAFAFA”>
<select name=”CETS_Dr”>
<option value=””>Select Doctor…</option>
<option value=”[[Doctor1]]”>[[Doctor1]]</option>
<option value=”[[Doctor2]]”>[[Doctor2]]</option>
<option value=”[[Doctor3]]”>[[Doctor3]]</option>
<option value=”[[Doctor4]]”>[[Doctor4]]</option>
</select></td>
</tr>

<tr>
<td><input type=”checkbox” name=”Appointment_Cards” id=”field2″ value=”Appointment Cards”></td>
<td>Appointment Cards</td>
<td><select name=”Appointment_Cards_QTY” id=”Item2″ onChange=”setPrice1(this)”>
<option value=””></option>
<option value=”500″>500 </option>
<option value=”1000″>1000</option>
<option value=”1500″>1500</option>
<option value=”2000″>2000</option>
<option value=”3000″>3000</option>
</select>
<input id=”AP” type=”hidden” name=”productid3″ value=””></td>
<td nowrap bgcolor=”FAFAFA”>
<select name=”Appointment_Cards_Type”>
<option value=””>Select Doctor…</option>
<option value=”[[Doctor1]]”>[[Doctor1]]</option>
<option value=”[[Doctor2]]”>[[Doctor2]]</option>
<option value=”[[Doctor3]]”>[[Doctor3]]</option>
<option value=”[[Doctor4]]”>[[Doctor4]]</option>
</select></td>
</tr>

<tr valign=”middle”>
<td colspan=”4″ align=”center”><input type=”submit” value=” Continue “></td>
</tr>

</table>

</td>
</tr>

</table>

</BODY>
</HTML>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NatdripMay 17.2007 — what you have here seems rather counter intuitive but here is the logic

either create an array that holds all the object names that you wish to check.

onSubmit()

go through the array check 2 see if the dropdown value != ""

if true then check to see if the corresponding check box is checked

or onChange() from the dropdown change the value of the check box to check if the dropdowns conditions of value != ""

there are more ways to do this I am sure but this should get you started
×

Success!

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

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

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