Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

&& and ¦¦ 1

Status
Not open for further replies.

Phonez

Technical User
Jan 16, 2002
58
GB
I have a form with two checkboxes on. I am trying to make it so either one or the other have to be selected before the form submits. I know it should be easy but I have probably been looking at too much code today to notice whats wrong

here is the code

Code:
<script language=&quot;JavaScript&quot;>
<!--
function sendforms() 
{
txt=document.form1.seminar.value;
txt1=document.form1.yes.value;
txt2=document.form1.no.value;

 if (txt.length == 0) {
 	 alert(&quot;Please select a seminar date&quot;);
    return;
 } 
 if (txt1 == &quot;Yes&quot; && txt2 == &quot;No&quot;) {
 	alert(&quot;Please select whether you would like to be kept informed on future seminars&quot;);
	return;
	}
 if (txt1 !== &quot;Yes&quot; && txt2 !== &quot;No&quot;){
 	alert(&quot;You have selected yes and no please select only one&quot;);
	return;
 } 
 document.form1.submit();
}
//-->
</script>

If anyone can help it would be much appreciated
 
I found some errors.
First, this:
[tt]txt1 !== &quot;Yes&quot; [/tt] is not correct, it should be or ==, or !=

Second, always add a pair of ( ) for each comparison to compound [tt]if [/tt] statements:
[tt]
if ( (txt1 == &quot;Yes&quot;) && (txt2 == &quot;No&quot;) )[/tt]


good luck
 
I have changed the code to

Code:
if ((txt1 == &quot;Yes&quot;) && (txt2 == &quot;No&quot;)) {
 	alert(&quot;Please select whether you would like to be kept informed on future seminars&quot;);
	return;
	}
 if ((txt1 != &quot;Yes&quot;) && (txt2 != &quot;No&quot;)) {
 	alert(&quot;You have selected yes and no please select only one&quot;);
	return;
 }

For some reason if you only select one it still alerts you and does not submit the form
 
hey in your requirement if user has to select &quot;tes&quot; or &quot;no&quot; then why r u using chck boxes .. use radio buttons.. they r ideal for uyour requirement. Put &quot;yes&quot; or &quot;no&quot; as default.

cheers!
srinu...
 
I am so stupid, I should have thought of that myself
 
It's hard to tell what exactly is the problem, because I really don't understand the logic completely. Does the user has to type &quot;yes&quot; or &quot;no&quot; in text fields? It doesn't make sense, why not to use a set of 2 radio buttons?

Also, I never you such kind of validations, but rather do this:

function sendforms() {

if [something is wrong]
return false; //form will not submit
...
else
return true; //form will submit
}

. . .
<form onsubmit=&quot;return(sendforms())&quot;>

This is the most safe way, and it makes debugging easier.

good luck
 
Its two checkboxes that I am using, one for yes and one for no. For some reason it always thinks that both checkboxes have been clicked even when they haven't
 
You are checking the value of the checkboxes, not whether or not they have been checked:

txt1=document.form1.yes.value;
txt2=document.form1.no.value;


Try something like this instead:

var txt1=&quot;&quot;
var txt2=&quot;&quot;
if(document.form1.yes.checked){
txt1=&quot;Yes&quot;
}
if(document.form1.no.checked){
txt2=&quot;No&quot;
}
if ((txt1 == &quot;&quot;) && (txt2 == &quot;&quot;)) {
alert(&quot;Please select whether you would like to be kept informed on future seminars&quot;);
return false;
}
if ((txt1 == &quot;Yes&quot;) && (txt2 == &quot;No&quot;)) {
alert(&quot;You have selected yes and no please select only one&quot;);
return false;
}
return true;
 
Thanks that is just what I wanted. It works fine now

You deserve a star for that
 
Darn, people! I just came along here (haven't looked at Tek-Tips for a few days) and while reading along came up with the .checked problem (before looking at JuanitaC's post). How could anyone overlook that! poo.

Oh and starway, you don't exactly need to keep all your conditions in parentheses. Just learn your operator precedence.

And good job, JuanitaC, for standing above the crowd ;-)
bluebrain.gif
blueuniment.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top