homepage Welcome to WebmasterWorld Guest from 54.227.20.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Form Validation
Is this possible?
ladyshanae

10+ Year Member



 
Msg#: 761 posted 8:39 pm on May 3, 2003 (gmt 0)

I am creating a form with 5 questions. Each question must be answered with a "yes" answer. Is it possible to use java script to validate that all answers are yes in order for the form to be submitted? If so . . . where can I find the code to do that? If not, is there another route i can take for the same effect? Thank you in advance!

 

korkus2000

WebmasterWorld Senior Member korkus2000 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 761 posted 8:52 pm on May 3, 2003 (gmt 0)

It is possible. Are you using radio buttons or check boxes?

ladyshanae

10+ Year Member



 
Msg#: 761 posted 9:07 pm on May 3, 2003 (gmt 0)

I'm currently using drop down menus, but I was using radio buttons before.

korkus2000

WebmasterWorld Senior Member korkus2000 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 761 posted 10:22 pm on May 3, 2003 (gmt 0)

Drop downs that have a yes and no option?

ladyshanae

10+ Year Member



 
Msg#: 761 posted 10:31 pm on May 3, 2003 (gmt 0)

Yes, they have a Select One, Yes, and No options.

But if it is easier to use radio buttons or check boxes, I can change it :)

korkus2000

WebmasterWorld Senior Member korkus2000 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 761 posted 10:49 pm on May 3, 2003 (gmt 0)

You can use a script like this:

<script language="JavaScript">
function checkSelects() {
//set a variable to hold the string info
var selectedNo = "";
//test the select boxes for their values
if (document.nameOfForm.NameOfDropDown1.options[document.nameOfForm.NameOfDropDown1.selectedIndex].value!= "yes") {
//If they are not equal to yes then add the text for the alert box of which
//select box is not equal to yes.
selectedNo += "\n - People";
}
if(document.nameOfForm.NameOfDropDown2.options[document.nameOfForm.NameOfDropDown2.selectedIndex].value!= "yes") {
selectedNo += "\n - Places";
}
if (document.nameOfForm.NameOfDropDown3.options[document.nameOfForm.NameOfDropDown3.selectedIndex].value!= "yes") {
selectedNo += "\n - Things";
}
//test to see if you have any string info in the variable
if (selectedNo!= "") {
//If so write an alert box telling which hasn't been selected to yes
selectedNo ="_____________________________\n" +
"You select no for:\n" +
selectedNo + "\n_____________________________" +
//Explain why you don't want these people submiting
"\nWe don't like negative people!\nSo we don't want you to submit\nyour form!";
alert(selectedNo);
//Stop the submit from going through
return false;
}
//No "no"s so let them submit.
else return true;
}
</script>

If you are not looking to tell the user anything then you could always do this:
<script language="JavaScript">
function checkSelects() {
//set a variable to hold the string info
var selectedNo = "";
//test the select boxes for their values
if (document.nameOfForm.NameOfDropDown1.options[document.nameOfForm.NameOfDropDown1.selectedIndex].value!= "yes") {
//If they are not equal to yes then add the text for the alert box of which
//select box is not equal to yes.
selectedNo += "\n - People";
}
if(document.nameOfForm.NameOfDropDown2.options[document.nameOfForm.NameOfDropDown2.selectedIndex].value!= "yes") {
selectedNo += "\n - Places";
}
if (document.nameOfForm.NameOfDropDown3.options[document.nameOfForm.NameOfDropDown3.selectedIndex].value!= "yes") {
selectedNo += "\n - Things";
}
//test to see if you have any string info in the variable
if (selectedNo!= "") {
//Stop the submit from going through
return false;
}
//No "no"s so let them submit.
else return true;
}
</script>

For the form it would look like this. Notice the onSubmit attribute in the opening form tag. It calls the function above. Make sure all names of the form and dropdowns are correct in the above function. You also need a submit button to fire off the onSubmit event.

<form name="nameOfForm" onSubmit="checkSelects()">
Do You Like People?
<select name="NameOfDropDown1">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do you like Places?
<select name="NameOfDropDown2">
<option value="no">No</option>
<option value="yes">Yes</option>
</select><br>
Do You Like Things?
<select name="NameOfDropDown3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<br>
<input type="submit">
</form>

BTW Welcome to WebmasterWorld. :)

ladyshanae

10+ Year Member



 
Msg#: 761 posted 11:14 pm on May 3, 2003 (gmt 0)

Thanks very much for the Welcome! One more question...how do I get it to also go to another url on submit, as well as validating the answers? Basically, I want the people who answer all yesses to be directed to another page after submission. The way that I did have it, people were getting directed to the other page no matter what they answered, once they hit the submit button.

I really liked the popup that your script gave me when someone answers no. Now I need the people who answer yes to get to go on to the next page. Is that possible?

Oaf357

10+ Year Member



 
Msg#: 761 posted 1:32 am on May 4, 2003 (gmt 0)

Do you have access to PHP?

korkus2000

WebmasterWorld Senior Member korkus2000 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 761 posted 1:46 am on May 4, 2003 (gmt 0)

So you want to redirect people who answer all yeses and not the nos? If so then I would handle that on the page you are submiting to. If they answer no they won't submit and it won't be a problem. You just set your action attribute in the form tag to the script or page processing the form like:

<form name="nameOfForm" onSubmit="checkSelects()" action="pageorscripthandlingform.cgi">

Then let that page or script do the redirect. Like Oaf357
mentioned serverside scripting will handle this much better than JavaScript.

ladyshanae

10+ Year Member



 
Msg#: 761 posted 3:46 am on May 4, 2003 (gmt 0)

I tried that, and it didn't work. When the user selects a no answer, the popup comes up. When they click "ok" on the popup, they are still redirected to the next page, lol. I am building this site for someone else, and I do know that they are going to be running something on there (unrelated to the site) that is ASP...I am not terribly sure if I can use php on this, if they are on a windows server and running ASP? This part is out of my league...all I am doing it building the plain, old fashioned html site, lol. That's why I wanted to use java script to do the validate/redirect if I could.

MWpro

10+ Year Member



 
Msg#: 761 posted 4:27 am on May 4, 2003 (gmt 0)

Using php to check if all of them are yes would be a lot easier using php. Where will this form submit to? Email? database?

Make a file ending with .php, put this in the file

<?
phpinfo();
?>

this will check if you have php on your server. If you do then we can help you with a nice php script to handle your problem.

DrDoc

WebmasterWorld Senior Member drdoc us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 761 posted 4:29 am on May 4, 2003 (gmt 0)

Then don't use a submit button.
Use a regular input button instead:

<input type="button" value="Submit" onclick="checkSelects()">

At the end of your JavaScript function (where you end up if they are all "yes") put this:

document.name_of_form.submit();

ladyshanae

10+ Year Member



 
Msg#: 761 posted 5:10 pm on May 4, 2003 (gmt 0)

Dr. Doc, that worked! Thank you SOOOOO much! I owe you my eternal gratitude, LOL! :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved