473,378 Members | 1,721 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,378 software developers and data experts.

Javascript validation loop problem

15
Hello, I’m running into a problem when I am trying to validate my form. I have a form that has 10 sections I need to validate with 70 inputs that the user must fill out. I tried doing it all in if statements but it only does half the checks and then breaks out of the function. I’m assuming I have reached and went beyond the number of if statements allowed. So what I am trying to do now is just run it through a loop and have the loop variable increment the input name on the form. Example I have 10 text boxes named TMName1, TMName2, TMName3 etc.. I'm not sure if it's even possilble the way I am trying to do it. This is the code I have.

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. function validate_form ( )
  3. {
  4. for(var x=1; x<10; x++)
  5. {
  6. if ( document.MainForm.TMName(x).value == "" )
  7. {
  8. alert ( "Please fill in the Team Member name." );
  9. document.MainForm.TMName(x).focus ( );
  10. return false;
  11. }
  12. if ( document.MainForm.FDepartment(x).selectedIndex == 0 )
  13. {
  14. alert ( "Please select the department " + document.MainForm.TMName(x).value + " is from." );
  15. document.MainForm.FDepartment(x).focus ( );
  16. return false;
  17. }
  18. if ( document.MainForm.FShift(x).selectedIndex == 0 )
  19. {
  20. alert ( "Please select the shift." );
  21. document.MainForm.FShift(x).focus ( );
  22. return false;
  23. }
  24. if ( document.MainForm.TDepartment(x).selectedIndex == 0 )
  25. {
  26. alert ( "Please select the department  " + document.MainForm.TMName(x).value + "  went to." );
  27. document.MainForm.TDepartment(x).focus ( );
  28. return false;
  29. }
  30. if ( document.MainForm.TShift(x).selectedIndex == 0 )
  31. {
  32. alert ( "Please select the shift" );
  33. document.MainForm.TShift(x).focus ( );
  34. return false;
  35. }
  36. if ( ( document.MainForm.FDepartment(x).selectedIndex == document.MainForm.TDepartment(x).selectedIndex ) && ( document.MainForm.FShift(x).selectedIndex == document.MainForm.TShift(x).selectedIndex ) )
  37. {
  38. alert ( "The From and To Shifts must be different." );
  39. document.MainForm.TShift(x).focus ( );
  40. return false;
  41. }
  42. if ( document.MainForm.STime(x).value == "" ) 
  43. {
  44. alert ( "Please enter the start time." );
  45. document.MainForm.STime(x).focus ( );
  46. return false;
  47. }
  48. }
  49. }
  50. </script>    
  51.  
Thanks,
John
Jan 16 '08 #1
1 1657
acoder
16,027 Expert Mod 8TB
Change, for example, document.MainForm.TMName(x).value to:
Expand|Select|Wrap|Line Numbers
  1. document.forms["MainForm"].elements["TMName"+x].value
and likewise for the rest.
Jan 16 '08 #2

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

Similar topics

111
by: Retlak | last post by:
The recommended (on dozens of websites) and effective (works in Netscape, MSIE, Mozilla, probably others) way to detect if a browser has Javascript turned off is to put this in the <head>: ...
3
by: Rob Meade | last post by:
Hi all, I have a login page which has username and password fields, a login button, and 2 validation controls (one for each field) - currently I have controls to display to the summary if the...
5
by: | last post by:
Hi all, Has anyone been able to write some custom javascript on the onclick event of submit button to do certain things like disable submit button, only submit form once etc. This was a breeze...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
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...
6
by: Peter Afonin | last post by:
Hello, I'm creating an application in ASP.NET 1.1. I need to check whether at least one checkbox in my datagrid has been checked. To do this, I'm using Javascript - I'm adding this code to...
5
by: Peter Afonin | last post by:
Hello, I'm not an expert in Javascript, so I'm seeking an advice. As I mentioned in my previous post, I use Javascript to check whether at least one checkbox in the datagrid has been checked....
6
by: lucoin | last post by:
Hello guys, I met a problem about php and javascipt For a tickets booking system, when a customer search for flights from one city, so in the data base there should be many routes to different...
13
by: Mtek | last post by:
Hi, We have a form defined with buttons like this: <a class="save_menu" href="javascript:document.Detail_Screen.action = 'savedata.php?screen=EDIT';document.Detail_Screen.submit();">Update</...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.