Hi. There's too many problems in your code...so I rewrite your code...
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Test</title>
-
</head>
-
<script language="javascript">
-
<!--
-
-
function validateForm(Qform)
-
{
-
var incorrect = new Array();
-
var no = 0;
-
for (var a = 0; a < Qform.elements.length; a++)
-
{
-
var field = Qform.elements[a];
-
-
if (field.value.length <= 0)
-
{
-
incorrect.push( field );
-
no ++;
-
}
-
else
-
{
-
field.style.backgroundColor="";
-
}
-
}
-
for( j=0; j<no; j++)
-
{
-
incorrect[j].style.backgroundColor="#FF0000";
-
}
-
-
if ( no > 0)
-
{
-
incorrect[0].focus();
-
return false;
-
}
-
else
-
{
-
return true;
-
}
-
}
-
-->
-
</script>
-
-
<body>
-
<form action="" method="post" onSubmit="return validateForm( this );">
-
<span id="1">Title: </span>
-
<input name="id" id="title" type="text" size="25" maxlength="6">
-
<br />
-
<span id="1">Content: </span><input name="content" id="content" type="text" size="25" maxlength="6">
-
<br />
-
<input id="submit" type="submit" value="OK" />
-
</form>
-
</body>
-
</html>
-
-
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.