View Full Version : simple dropdown (i think!)
mimpoz
02-24-2004, 12:41 PM
I have made a dropdown that I need a textbox to appear when user clicks one of the last 2 options.
Problem is that it only works when the user clicks on the second last one. How can i do a check that looks to see if either options are selected??
Here is the URL: http://www.chavaroth.com/form.htm
Thanks for any help!
agent002
02-24-2004, 12:48 PM
I found your code rather illogical, so I have redone it quite completely.
function chkOther(selval, field){
if(!selval){
return false;
}
else {
var undisable = false;
if(selval == 'tzedakah'){
undisable = true;
field.value = 'Which Tzedakah Fund?';
}
else if(selval == 'other'){
undisable = true;
field.value = 'Specify please';
}
field.className = undisable? 'visible' : 'invisible';
field.disabled = undisable? false : true;
if(undisabled) field.focus();
}
mimpoz
02-24-2004, 12:57 PM
hey - wow!
slight problem - now i'm getting another error: 'undisabled' is undefined
???
agent002
02-24-2004, 01:14 PM
sorry, made a small mistake. but this should do:
function chkOther(selval, field){
if(!selval){
return false;
}
else {
var undisable = false;
if(selval == 'tzedakah'){
undisable = true;
field.value = 'Which Tzedakah Fund?';
}
else if(selval == 'other'){
undisable = true;
field.value = 'Specify please';
}
field.className = undisable? 'visible' : 'invisible';
field.disabled = undisable? false : true;
if(undisable) field.focus();
}
mimpoz
02-24-2004, 01:19 PM
Thanks a million for your help!!
mimpoz
02-24-2004, 01:20 PM
one more thing - do you know how to make the text box mandatory??
agent002
02-24-2004, 01:35 PM
You want the script to give an alert if the field(s) are empty? Okay, I can do that. As you already have the following attribute in the <form> tag:
onSubmit="return Form1_onsubmit(this)"
I will just need to do the JavaScript for it:
function Form1_onsubmit(fh){
if(fh.heardAboutUs.value == ''){
alert('Please choose an option first.');
return false;
}
if(fh.heardAboutUs.value == 'tzedakah' || fh.heardAboutUs.value == 'other'){
if(fh.tzedaka.value == ''){
alert('Please describe the option you chose.');
return false;
}
}
return true;
}
agent002
02-24-2004, 01:36 PM
Oh, and please remember that this is no 100% safe way of validating the form, as the viewer could have disabled JavaScript in his browser. So also have your serverside script (which you are submitting the data to) verify that the values aren't empty.
mimpoz
02-24-2004, 01:48 PM
I plugged it into the real page - what did i do wrong??
http://chavaroth.com/contact2.asp?id=1
agent002
02-24-2004, 02:05 PM
First off:
<select onchange="chkOther(this.options[selectedIndex].value,tzedaka)" name="heardAboutUs">
Second. I see you have changed my code a bit. I don't know exactly what's causing the first error (syntax error), but I have the feeling it's this code. Please try removing this:
else
; // Information automatically sent to server
That syntax error is also causing the second error (when changing the selection of the <select> menu).
agent002
02-24-2004, 02:09 PM
After another look, I noticed that you are also missing a right bracket here (added in blue). This is the syntax error:
//new code
else if (theForm.heardAboutUs.selectedIndex == "tzedakah" || theForm.heardAboutUs.selectedIndex == "other") {
if (theForm.tzedaka.value == ""){
alert("Please describe the option you chose.");
return (false);
}
}
//end new code
mimpoz
02-24-2004, 03:43 PM
Thanks for finding that error. But now it doesn't give me an alert if the field is empty? Any ideas?
agent002
02-25-2004, 09:07 AM
You don't have my code implemented anywhere in Form1_onsubmit().
This is the code, I enhanced it a bit (blue is new):
function Form1_onsubmit(fh){
if(fh.heardAboutUs.value == ''){
alert('Please choose an option first.');
return false;
}
if(fh.heardAboutUs.value == 'tzedakah' || fh.heardAboutUs.value == 'other'){
if(fh.tzedaka.value == '' || fh.tzedaka.value == 'Which Tzedakah Fund?' || fh.tzedaka.value == 'Specify please'){
alert('Please describe the option you chose.');
return false;
}
}
return true;
}
mimpoz
02-25-2004, 10:03 AM
i put up just the code you sent me - http://www.chavaroth.com/form.htm and it works fine, but when i put it into the contact2.asp it doesn't give me an error if the field is blank...
So sorry for driving you crazy.
agent002
02-25-2004, 10:16 AM
You're not driving me crazy... :)
I implemented the code for you, the complete JavaScript part is attached to this post.
mimpoz
02-25-2004, 11:05 AM
thanks a million for that! all works great - http://www.chavaroth.com/contact.asp
have a good one!
agent002
02-25-2004, 11:19 AM
no problem :)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.