2014-01-31 25 views
1

你好,當我得到一些驗證錯誤,如果我重置我的表單錯誤仍然在頁面上我怎麼能重置錯誤以及輸入字段。請幫助我在這如何重置驗證jsp頁面按下重置按鈕時顯示的錯誤

剛剛javascript我該怎麼寫在resetForm()方法?

<form action="RegisterServlet" method="post" name="Register" id="signup" onSubmit="return validate()"> 
<table> 
<tr> 
<td>First Name* : </td> 
<td><input type="text" name="txtFname" id="fname" maxlength="30" onKeyup="capitalize(this)"/><br/> 
<span id="errorFirstNameMissing" style="display:none;"><font color="red">*Please provide your first name.</font></span> 
<span id="errorFirstNameInValid" style="display:none;"><font color="red">*Please provide a valid first name.</font></span> 
</td> 
</tr> 
<tr> 
<td>Last Name* : </td> 
<td><input type="text" name="txtLname" id="lname" maxlength="30" onKeyup="capitalize(this)"/><br/> 
<span id="errorLastNameMissing" style="display:none;"><font color="red">*Please provide your Last name.</font></span> 
<span id="errorLastNameInValid" style="display:none;"><font color="red">*Please provide a valid Last name.</font></span> 
</td> 
</tr> 
<tr> 
<td>Gender* : </td> 
<td><select name = "txtGender" id="gender"> 
<option value="unknown">Select your Gender</option> 
<option value="Male">Male</option> 
<option value="Female">Female</option> 
</select><br/> 
<span id="errorMissingGender" style="display:none;"><font color="red">*Please select a Gender.</font></span> 
</td> 
</tr> 
<tr> 
<td>Category* : </td> 
<td><select name = "txtCategory" id="category"> 
<option value="unknown">Select your Category</option> 
<option value="Affiliate">Affiliate</option> 
<option value="Client">Client</option> 
<option value="Admin">Admin</option> 
</select><br/> 
<span id="errorMissingCategory" style="display:none;"><font color="red">*Please select a Category.</font></span> 

</td> 
</tr> 
<tr> 
<td><script type="text/javascript" 
src="js/Calendar.js"></script> 
</td> 
</tr> 

<tr> 
<td>Age :</td> 
<td><input type=text name=txtAge id="ageId" readonly style="width: 20px;background-color:#D0D0D0;border:none"/>yrs.</td> 
</tr> 

<tr> 
<td>Address* : </td> 
<td><textarea rows="5" name="txtAddr" id="addr" cols="30"></textarea><br/> 
<span id="errorMissingAddress" style="display:none;"><font color="red">*Please provide a valid Address.</font></span> 
</td> 
</tr> 
<tr> 
<td>State* :</td> 
<td> <select onchange="print_city('city',this.selectedIndex);" id="state" name ="txtState"></select><br/> 
<span id="errorMissingState" style="display:none;"><font color="red">*Please select a state.</font></span> 

</td> 
</tr> 
<tr> 
<td>City* :</td> <td><select name ="txtCity" id ="city"></select> 
     <script language="javascript">print_state("state");</script><br/> 
<span id="errorMissingCity" style="display:none;"><font color="red">*Please select a city.</font></span> 
</td> 
</tr> 

<tr> 
<td>Pincode* : </td> 
<td><input type="text" name="txtPin" id="pin"/><br/> 
<span id="errorMissingPinCode" style="display:none;"><font color="red">*Please provide your Pincode.</font></span> 
<span id="errorPinCodeInvalid" style="display:none;"><font color="red">*Please provide a valid Pincode.</font></span> 
</td> 
</tr> 
<tr> 
<td>e-Mail* : </td> 
<td><input type="text" name="txtEmail" id="email"/><br/> 
<span id="errorMissingEmail" style="display:none;"><font color="red">*Please provide your emailId.</font></span> 
<span id="errorEmailInvalid" style="display:none;"><font color="red">*Please provide a valid emailId.</font></span> 
</td> 
</tr> 
<tr> 
<td>Contact Number : </td> 
<td><input type="text" name="txtStd" id="std" maxlength="6" style="width: 40px"/>-<input type="text" name="txtPhone" id="phone" maxlength="8"/><br/> 
<span id="errorStdCodeInvalid" style="visibility:hidden;"><font color="red">*Please provide a valid std code.</font></span> 
<span id="errorPhoneNoInvalid" style="visibility:hidden;"><font color="red">*Please provide a valid contact no.</font></span> 
</td> 
</tr> 

<tr> 
<td>Mobile Number* : </td> 
<td>+91-<input type="text" name="txtMobile" id="mobile" maxlength="10"/><br/> 
<span id="errorMissingMobileNo" style="display:none;"><font color="red">*Please provide your mobile number.</font></span> 
<span id="errorMobileNoInvalid" style="display:none;"><font color="red">*Please provide a valid mobile number.</font></span> 
</td> 
</tr> 
</table> 
<br/> 
<p><font color="red">Note: All the fields marked with * are mandatory.</font></p> 
<p><input type="checkbox" name="chkAgree" onclick="EnableSubmit(this)" /><font color="green"> I here by declare that the above data entered by me is true to my knowledge.</font> </p> 
<br/> 

<div class="style2"> 
<table> 
<tr> 
<td><button type="submit" id="submit" disabled style="width: 80px;height: 40px">Submit</button></td> 
<td><div class="divider"></div></td> 
<td><button type="reset" style="width: 80px;height: 40px" onClick="resetForm()">Reset</button></td> 
</tr> 
</table> 
</div> 
</form> 

回答

1

我加入到這個功能resetForm()

function resetForm(){ 
document.getElementById('errorFirstNameMissing').style.display='none'; 
document.getElementById('errorFirstNameInValid').style.display='none'; 
document.getElementById('errorLastNameMissing').style.display='none'; 
document.getElementById('errorLastNameInValid').style.display='none'; 
document.getElementById('errorMissingCategory').style.display='none'; 
document.getElementById('errorMissingGender').style.display='none'; 
document.getElementById('errorMissingDOB').style.display='none'; 
document.getElementById('errorDateInvalid').style.display='none'; 
document.getElementById('errorMissingAddress').style.display='none'; 
document.getElementById('errorMissingState').style.display='none'; 
document.getElementById('errorMissingCity').style.display='none'; 
document.getElementById('errorMissingPinCode').style.display='none'; 
document.getElementById('errorPinCodeInvalid').style.display='none'; 
document.getElementById('errorMissingEmail').style.display='none'; 
document.getElementById('errorEmailInvalid').style.display='none'; 
document.getElementById('errorStdCodeInvalid').style.display='none'; 
document.getElementById('errorPhoneNoInvalid').style.display='none'; 
document.getElementById('errorMissingMobileNo').style.display='none'; 
document.getElementById('errorMobileNoInvalid').style.display='none'; 

} 
0

我有同樣的問題解決它!我添加了這個功能,併爲我工作得很好。

function resetForm(){ 
document.getElementById('errorFirstNameMissing').innerHTML=""; 
document.getElementById('errorFirstNameInValid').innerHTML=""; 
document.getElementById('errorLastNameMissing').innerHTML=""; 
document.getElementById('errorLastNameInValid').innerHTML=""; 
document.getElementById('errorMissingCategory').innerHTML=""; 
document.getElementById('errorMissingGender').innerHTML="" 
document.getElementById('errorMissingDOB').innerHTML=""; 
document.getElementById('errorDateInvalid').innerHTML=""; 
document.getElementById('errorMissingAddress').innerHTML=""; 
document.getElementById('errorMissingState').innerHTML=""; 
document.getElementById('errorMissingCity').innerHTML=""; 
document.getElementById('errorMissingPinCode').innerHTML=""; 
document.getElementById('errorPinCodeInvalid').innerHTML=""; 
document.getElementById('errorMissingEmail').innerHTML=""; 
document.getElementById('errorEmailInvalid').innerHTML=""; 
document.getElementById('errorStdCodeInvalid').innerHTML=""; 
document.getElementById('errorPhoneNoInvalid').innerHTML=""; 
document.getElementById('errorMissingMobileNo').innerHTML=""; 
document.getElementById('errorMobileNoInvalid').innerHTML=""; 

}