473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

form validation problems

meenakshia
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
Sep 2 '08 #1
7 1193
acoder
16,027 Expert Mod 8TB
Call the validation function onsubmit:
Expand|Select|Wrap|Line Numbers
  1. <form ... onsubmit="return formCheck()">
What does saveForm() do?
Sep 3 '08 #2
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
Sep 3 '08 #3
acoder
16,027 Expert Mod 8TB
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.
Sep 3 '08 #4
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:(
Sep 4 '08 #5
acoder
16,027 Expert Mod 8TB
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().
Sep 4 '08 #6
hi
thanks a lot
yes problem is solved:)
thanke a ton:)
smile always
anand
Sep 9 '08 #7
acoder
16,027 Expert Mod 8TB
No problem, you're welcome :)
Sep 9 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
1
by: Suzanne Murray | last post by:
Hi, I am trying to provide some simple validation using Javascript on my form. However at times the validation works and at others it doesn't and submits the form without any warnings even...
10
by: Steve Benson | last post by:
Our regular programmer moved on. I'm almost clueless in Javascript/ASP and got the job of adapting existing code. In the page below, everything works until I added the function checkIt() to...
5
by: billa | last post by:
I wish to carry out standard form validation (i.e. is it a date?, is there a value in the field, is it a number) using the onBlur event rather than the onSubmit event. This (of course) leads to...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
8
by: Phil Latio | last post by:
I've been creating an application over the last few weeks and generally pleased with what I have produced but one area is irritating me, form validation. At the moment the forms are simply...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
12
by: Gustaf | last post by:
I've been working on a membership form for a while, and find it very tedious to get an acceptable level of form validation. A web search for solutions revealed some home-brewed solutions, such as...
6
by: smk17 | last post by:
I've spent the last few minutes searching for this question and I found an answer, but it wasn't quite what the client wanted. I have a simple online form where the user needs to fill out five...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.