|
|
Subject:
Javascript onsubmit form validation:
Category: Computers > Programming Asked by: knoxville_jeff-ga List Price: $25.00 |
Posted:
14 Mar 2004 09:17 PST
Expires: 13 Apr 2004 10:17 PDT Question ID: 316595 |
I am trying to create a form to submit one field to an email recipient (test@test.com in the below code). I am looking to have the act of submitting be dependent on the choices the customer selects from pull down menus on the same page. That is to say if the customer answers the questions according to the acceptable criteria their email address will be submitted. If they do not meet the criteria they will be sent to a page saying sorry we can't help you (http://test/sorry.htm). Otherwise if they do meet the criteria the email address field data will be sent to test@test.com and the customer will be directed to the confirmation page (http://test/thanks.htm). I have set up the form, however I do not have the validation javascript figured out. The criteria to get submitted is all or nothing, any non acceptable answer will give them the sorry page but to get submitted all drop down questions must be one of the acceptable answers. All questions must answered (the first choice is not acceptable in pull downs) Question 1 answer1= not acceptable answer2= acceptable Question 2 answer1= not acceptable answer2= acceptable Question 3 answer1= acceptable answer2= acceptable answer3= not acceptable answer4= not acceptable Question 4 answer1= acceptable answer2= acceptable answer3= acceptable answer4= not acceptable answer5= not acceptable email address must be filled out. Also if I am not already asking too much would it be possible to allow the customer to only click the submit button once (in frontpage code) Thanks for all of your help: (what I have so far... sorry about frontpage code): <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta name="GENERATOR" content="Microsoft FrontPage 5.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> <meta name="Microsoft Border" content="b, default"> </head> <body> <form method="POST" action="--WEBBOT-SELF--"> <!--webbot bot="SaveResults" S-Label-Fields="TRUE" U-File="/private/results.csv" S-Format="TEXT/CSV" S-Email-Address="test@test.com" S-Email-Format="TEXT/PRE" --><table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="63%" id="AutoNumber7"> <tr> <td width="69%" colspan="3">Question 1</td> <td width="31%"><select size="1" name="D1"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 2</td> <td width="31%"><select size="1" name="D2"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 3</td> <td width="31%"><select size="1" name="D3"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> <option>answer3</option> <option>answer4</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 4</td> <td width="31%"><select size="1" name="D4"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> <option>answer3</option> <option>answer4</option> <option>answer5</option> </select></td> </tr> <tr> <td width="3%"> </td> <td width="2%">A</td> <td width="64%">Answer description 1</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">B</td> <td width="64%">Answer description 2</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">C</td> <td width="64%">Answer description 3</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">D</td> <td width="64%">Answer description 4</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">F</td> <td width="64%">Answer description 5</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%"> </td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%">e-mail address: <input type="text" name="T1" size="20"></td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%"> </td> <td width="31%"> </td> </tr> </table> <p><input type="submit" value="Submit" name="B1"></p> </form> </body> </html> | |
| |
| |
|
|
Subject:
Re: Javascript onsubmit form validation:
Answered By: hammer-ga on 15 Mar 2004 07:30 PST Rated: |
Jeff, I tried everything I could think of to get your form to redirect on fail without also submitting the form. Unfortunately, I was unable to find a solution that would work on all browsers, while also handling the Enter key, etc. The code below performs all your validations. It provides a different error message when the user has not made all selections, as opposed to making invalid selections. It should also prevent multiple submission, in most cases. <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta name="GENERATOR" content="Microsoft FrontPage 5.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>New Page 1</title> <meta name="Microsoft Border" content="b, default"> <script language="Javascript"> <!-- function validateMe() { var failed = false; if(document.my_form.submitted) return false; if((document.my_form.D1.selectedIndex == 0) || (document.my_form.D2.selectedIndex == 0) || (document.my_form.D3.selectedIndex == 0) || (document.my_form.D4.selectedIndex == 0)) { failed = true; alert('You must make a selection from each choice list.'); } else { if((document.my_form.D1.selectedIndex == 1) || (document.my_form.D2.selectedIndex == 1) || (document.my_form.D3.selectedIndex == 3) || (document.my_form.D3.selectedIndex == 4) || (document.my_form.D4.selectedIndex == 4) || (document.my_form.D4.selectedIndex == 5)) { failed = true; alert('Sorry, we cannot help you.'); } } if(failed) return false; else return true; } //--> </script> </head> <body> <form method="POST" name="my_form" onSubmit="return validateMe()" action="--WEBBOT-SELF--"> <!--webbot bot="SaveResults" S-Label-Fields="TRUE" U-File="/private/results.csv" S-Format="TEXT/CSV" S-Email-Address="test@test.com" S-Email-Format="TEXT/PRE" --><table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="63%" id="AutoNumber7"> <tr> <td width="69%" colspan="3">Question 1</td> <td width="31%"><select size="1" name="D1"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 2</td> <td width="31%"><select size="1" name="D2"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 3</td> <td width="31%"><select size="1" name="D3"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> <option>answer3</option> <option>answer4</option> </select></td> </tr> <tr> <td width="69%" colspan="3">Question 4</td> <td width="31%"><select size="1" name="D4"> <option>- - - -</option> <option>answer1</option> <option>answer2</option> <option>answer3</option> <option>answer4</option> <option>answer5</option> </select></td> </tr> <tr> <td width="3%"> </td> <td width="2%">A</td> <td width="64%">Answer description 1</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">B</td> <td width="64%">Answer description 2</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">C</td> <td width="64%">Answer description 3</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">D</td> <td width="64%">Answer description 4</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%">F</td> <td width="64%">Answer description 5</td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%"> </td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%">e-mail address: <input type="text" name="T1" size="20"></td> <td width="31%"> </td> </tr> <tr> <td width="3%"> </td> <td width="2%"> </td> <td width="64%"> </td> <td width="31%"> </td> </tr> </table> <p><input type="submit" value="Submit" name="B1"></p> </form> </body> </html> Please ask for clarification if you need anything explained. Resources: IRT.org Javascript FAQ http://developer.irt.org/script/script.htm - Hammer | |
| |
|
knoxville_jeff-ga
rated this answer:
Thanks you are great! |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |