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

[Javascript] Change color in the field

100+
P: 263
Hi all.

This code javascript not working... nothing error but not working... :(

I need change color in the fields of the form when fields is null...

My code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function validateForm(Qform) 
  3. {
  4.  
  5.   for (var a = 0; a < Qform.elements.length; a++) 
  6.  
  7.     {
  8.  
  9.     var field = Qform.elements[a];
  10.     var incorrect = new Array();
  11.         var no = 0;
  12.  
  13.           if (field.value.length <= 0) 
  14.         {
  15.             window.alert('K.O. !');
  16.             field.focus();
  17.             return false;
  18.  
  19.               } else { 
  20.                 incorrect[no] = "1";
  21.                 no++;
  22.  
  23.  
  24.                   }
  25.  
  26. }
  27.  
  28. for(j=0;j<no;j++) {
  29.           document.getElementById(incorrect[j]).style.color="#FF0000";
  30.   }
  31.  
  32. ....
  33.  
  34. <form action="" method="post" onSubmit="validateForm();">
  35.  
  36. <span id="1">Title </span>
  37. <input name="id" id="title" type="text" size="25" maxlength="6">
  38.  
  39. ....
  40.  
  41.  
Aug 29 '08 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
some thought about the code

- you define validateForm() with a parameter (an object) but onsubmit there's no parameter given
- when defining the incorrect[] array elements you should assign the appropriate id otherwise getElementById() won't return something
- you redefine incorrect[] everytime you loop through the array, I think you should initialize it outside the for loop
Aug 29 '08 #2

Ferris
100+
P: 101
Hi. There's too many problems in your code...so I rewrite your code...

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Test</title>
  6. </head>
  7. <script language="javascript">
  8. <!--
  9.  
  10. function validateForm(Qform)
  11. {
  12.     var incorrect = new Array();
  13.     var no = 0;
  14.     for (var a = 0; a < Qform.elements.length; a++)
  15.     {
  16.         var field = Qform.elements[a];
  17.  
  18.         if (field.value.length <= 0)
  19.         {
  20.             incorrect.push( field );
  21.             no ++;
  22.         }
  23.         else
  24.         {
  25.             field.style.backgroundColor="";
  26.         }
  27.     }
  28.     for( j=0; j<no; j++) 
  29.     {
  30.         incorrect[j].style.backgroundColor="#FF0000";
  31.     }
  32.  
  33.     if ( no > 0)
  34.     {
  35.         incorrect[0].focus();
  36.         return false;
  37.     }
  38.     else
  39.     {
  40.         return true;
  41.     }
  42. }
  43. -->
  44. </script>
  45.  
  46. <body>
  47. <form action="" method="post" onSubmit="return validateForm( this );">
  48. <span id="1">Title: </span>
  49. <input name="id" id="title" type="text" size="25" maxlength="6">
  50. <br />
  51. <span id="1">Content: </span><input name="content" id="content" type="text" size="25" maxlength="6">
  52. <br />
  53. <input id="submit" type="submit" value="OK" />
  54. </form>
  55. </body>
  56. </html>
  57.  
  58.  

I use incorrect array to save invalid field object,not index as you did. and pay attention to

onSubmit="return validateForm( this );

you should pass a form object into validateForm,and the function must return a bool value, return false means the form will NOT be submit. if you didn't return a value,the form will be submit everytime.


hope it helps.
Aug 30 '08 #3

100+
P: 263
Many thanks x your great help !!!
Kind regards
Viki
Aug 31 '08 #4

Post your reply

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