SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [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:

    Code:
    function validateForm(Qform) 
    {
    
      for (var a = 0; a < Qform.elements.length; a++) 
      
    	{
    	
    	var field = Qform.elements[a];
    	var incorrect = new Array();
            var no = 0;
    
          	if (field.value.length <= 0) 
    		{
            	window.alert('K.O. !');
            	field.focus();
            	return false;
            	
          		} else { 
       	         incorrect[no] = "1";
       	         no++;
         		
          		    
          		    }
          		    
    }
    
    for(j=0;j<no;j++) {
      		document.getElementById(incorrect[j]).style.color="#FF0000";
      }
    
    ....
    
    <form action="" method="post" onSubmit="validateForm();">
    
    <span id="1">Title </span>
    <input name="id" id="title" type="text" size="25" maxlength="6">
    
    ....

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function validateForm(Qform){
    	var i=0,Q= Qform.elements,L=Q.length;
    	for (var a = 0; a < L; a++){
    		var field = Q[a];
    		if(field.value==='' && field.type=='text'){
    			field.style.backgroundColor= 'red';
    			++i;
    		}
    	}
    	if(i) {
    		alert('You must provide values for all the fields');
    		return false;
    	}
    	return true;
    }

  3. #3
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks mrhoo x your reply.

    Your script working for all fields in the form type text, but not working for all fields type <select size="1".... >

    And is possible when the field was compiled return to original color ?

    can you help me?
    Last edited by viki1967; Aug 29, 2008 at 08:34.

  4. #4
    Non-Member
    Join Date
    Jul 2005
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if(field.value==='' && (field.type=='text' || field.type=='select-one')){

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function validateForm(Qform){
    	var i=0,Q= Qform.elements,L=Q.length;
    	for (var a = 0; a < L; a++){
    		var field = Q[a];
    		if(field.value===''){
    			field.style.backgroundColor='red';
    			++i;
    		}
    		else field.style.backgroundColor='';
    	}
    	if(i){
    		alert('You must provide values for all the fields');
    		return false;
    	}
    	return true;
    }

  6. #6
    Non-Member
    Join Date
    Aug 2007
    Posts
    494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many thanks !!!



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •