do not use "document.write", use some other means like hiddent content and inserting html code with insertAdjacentHTML or innerHTML, here is an example, also see my response to your other post:<style type="text/css">
FORM SPAN {color:red;}
</style>
<form name="frmtest" onsubmit="return checkInput()">
<div id="dvPrompt">Enter the following fields:</div>
Field 1:<input type="text" name="txt1"><br>
Field 2:<input type="text" name="txt2"><br>
<input type="submit" value="Submit">
</form>
<script language="javascript">
function checkInput()
{
 var elements = document.frmtest.elements;
 var nErr = 0;
 for (var i=0; i < elements.length;i++)
 {
if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") 
if (elements[i].value.length == 0)
{
if (elements[i].nextSibling.tagName != "SPAN")
{
elements[i].insertAdjacentHTML("afterEnd","<span>*</span>");
} elements[i].nextSibling.style.display="";
++nErr;
}
else
{
if (elements[i].nextSibling.tagName == "SPAN")
{
elements[i].nextSibling.style.display="none";
}
}
 } if (nErr > 0)
 {
document.all("dvPrompt").innerHTML = "Please enter a valid value for fields with <span>*</span>:";
return false;
 }
 return true;
 
}
</script>

解决方案 »

  1.   

    thank you very much! But there is till a little problem: When I submit the form
    with some error input, the indicative information can only be displayed for one
    second and then disappear.I change the button type to "button" and then it is ok. I think I cannot prevent the form from being submitted. How to deal with this relasionship between checking and submitting,could you tell me? I will be
    very appreciated!
      

  2.   

    if there is an error, the form will never be submitted, since the return value of checkInput() is "false", what browser are you using? 
      

  3.   

    did you get problems when you were using the exact code I posted above or you had your own code? if it is the latter, can you post the code?
      

  4.   

    I am very sorry that I made a big mistake. I forgot write "return" in front
    of the CheckInput() function. Thank you very much and sorry for bothering
    you too much.