Connecting Tech Pros Worldwide Help | Site Map

form validation problems

  #1  
Old September 2nd, 2008, 06:59 PM
meenakshia's Avatar
Member
 
Join Date: Jun 2008
Location: delhi,india
Posts: 33
hi forum
i have been trying to use a way to validate my form but i was unable to use it properly
pls rectify my mistakes.

i m using input type as a button and calling a function to save the form data.
now i want to check if all fields are entered properly before submitting the form

the javascript function i m using is

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. function formCheck(formobj){
  3.     // Enter name of mandatory fields
  4.     var fieldRequired = Array("txtddate", "txtspid");
  5.     // Enter field description to appear in the dialog box
  6.     var fieldDescription = Array("Delivery Date", "Sales Person Name");
  7.     // dialog message
  8.     var alertMsg = "Please complete the following fields:\n";
  9.  
  10.     var l_Msg = alertMsg.length;
  11.  
  12.     for (var i = 0; i < fieldRequired.length; i++){
  13.         var obj = formobj.elements[fieldRequired[i]];
  14.         if (obj){
  15.             switch(obj.type){
  16.             case "select-one":
  17.                 if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
  18.                     alertMsg += " - " + fieldDescription[i] + "\n";
  19.                 }
  20.                 break;
  21.             case "select-multiple":
  22.                 if (obj.selectedIndex == -1){
  23.                     alertMsg += " - " + fieldDescription[i] + "\n";
  24.                 }
  25.                 break;
  26.             case "text":
  27.             case "textarea":
  28.                 if (obj.value == "" || obj.value == null){
  29.                     alertMsg += " - " + fieldDescription[i] + "\n";
  30.                 }
  31.                 break;
  32.             default:
  33.             }
  34.             if (obj.type == undefined){
  35.                 var blnchecked = false;
  36.                 for (var j = 0; j < obj.length; j++){
  37.                     if (obj[j].checked){
  38.                         blnchecked = true;
  39.                     }
  40.                 }
  41.                 if (!blnchecked){
  42.                     alertMsg += " - " + fieldDescription[i] + "\n";
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     if (alertMsg.length == l_Msg){
  49.         return true;
  50.     }else{
  51.         alert(alertMsg);
  52.         return false;
  53.     }
  54. }
  55. </script>
  56.  
and form details are
Expand|Select|Wrap|Line Numbers
  1. <form name="outputForm1" >
  2. <td>Delivery Date</td>
  3. <td><input type="text" name="txtddate" tabindex="1"  size="20"  ></td>
  4. <td>Sales Person Name</td>
  5. <td><select name="txtspid" tabindex="2">
  6.     <option selected>Pls. Choose</option>
  7.     <option value="1">Hemant</option>
  8.     <option value="2">Rajan</option>
  9.     <option value="3">Ranjan Kumar</option>
  10.     <option value="4">Sanjay Parwani</option>
  11.     <option value="5">Santosh</option>
  12.     <option value="6">Rajesh</option>
  13.     </select>
  14. </td>
  15. <td><input type="button" value="Save" name="btnSave" tabindex="21" onclick = "SaveForm();"></td>
  16.  
  17.  
if you want to see the save form function,pls let me know i will post it seperately.

i m not able to put the function before the saveform function runs
pls advice how i should go ahead:)

smile always:)
anand
  #2  
Old September 3rd, 2008, 11:24 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: form validation problems


Call the validation function onsubmit:
Expand|Select|Wrap|Line Numbers
  1. <form ... onsubmit="return formCheck()">
What does saveForm() do?
  #3  
Old September 3rd, 2008, 05:40 PM
meenakshia's Avatar
Member
 
Join Date: Jun 2008
Location: delhi,india
Posts: 33

re: form validation problems


hi thanks for ur reply
well actually i have posted only a part of the whole code
it is too big
and there are around 10 input fields for which the user has to input the data in
and saveform function saves that data in ms access file(the database)
my problem was that i was not able to use both saveform and form validator which is really important for the working of the whole form
i will try and get back to u if it works fine:)
smile always:)
anand
  #4  
Old September 3rd, 2008, 05:43 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: form validation problems


Does SaveForm() save to a local MS Access file or does it submit to the server?

You can call SaveForm() from within the form validation function if it passes validation.
  #5  
Old September 4th, 2008, 04:29 AM
meenakshia's Avatar
Member
 
Join Date: Jun 2008
Location: delhi,india
Posts: 33

re: form validation problems


hi yes it saves the data on local machine.there is no server and yes that is what i m trying to do without success
i m trying to put saveform() inside the validation function but unable to run it:(
  #6  
Old September 4th, 2008, 11:19 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: form validation problems


OK, make the save button into a submit button and remove the onclick. Add the onsubmit if you haven't already done so. Now in the formCheck() function where you would return true, call SaveForm() instead. To prevent form submission, you should also return false at the end of SaveForm().
  #7  
Old September 9th, 2008, 07:13 PM
meenakshia's Avatar
Member
 
Join Date: Jun 2008
Location: delhi,india
Posts: 33

re: form validation problems


hi
thanks a lot
yes problem is solved:)
thanke a ton:)
smile always
anand
  #8  
Old September 9th, 2008, 08:53 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: form validation problems


No problem, you're welcome :)
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form validation question Hosh answers 16 October 24th, 2005 09:45 AM
cgi form validation problems googleboy answers 1 August 15th, 2005 12:55 PM
Form Validation problems Suzanne Murray answers 1 July 19th, 2005 08:11 AM
Automated Form Validation? Stefan Richter answers 21 July 17th, 2005 12:47 PM