By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

Javascript validation loop problem

P: 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
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
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

Post your reply

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