Connecting Tech Pros Worldwide Help | Site Map

[Javascript] Change color in the field

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 29th, 2008, 01:28 PM
Familiar Sight
 
Join Date: Oct 2007
Posts: 226
Default [Javascript] Change color in the field

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.  
Reply
  #2  
Old August 29th, 2008, 08:38 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Age: 32
Posts: 2,209
Default

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
Reply
  #3  
Old August 30th, 2008, 11:45 AM
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Age: 23
Posts: 102
Default

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.
Reply
  #4  
Old August 31st, 2008, 08:54 AM
Familiar Sight
 
Join Date: Oct 2007
Posts: 226
Default

Many thanks x your great help !!!
Kind regards
Viki
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.