[Javascript] Change color in the field

Hi all.

This code javascript not working… nothing error but not working… :frowning:

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

My 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">

....


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;
}

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?

if(field.value==='' && (field.type=='text' || field.type=='select-one')){
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;
}

many thanks !!!

:wink: