javascript - 如何重置按下重置按钮时 jsp 页面上显示的验证错误

标签 javascript 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>

最佳答案

我通过将其添加到函数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';

}

关于javascript - 如何重置按下重置按钮时 jsp 页面上显示的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477813/

相关文章:

javascript - 模块构建失败 : Error: write EPIPE (or make processCssUrls:false) in css imports Laravel Mix

java - RequestDispatcher 无法从 servlet doPost 到其他 servlet doGet

java - 在 Struts2 中使用 AJAX 根据另一个选择菜单填充一个选择菜单

Java Servlet 和 JSP,如何从文件中获取完整路径名?

java - 如何处理servlet上没有请求

javascript - 不要在页面刷新时启动tinymce

javascript - 为什么这段来自 JavaScript 的简单代码会错误地进行变量提升?

javascript - setInterval() 只运行一次函数

javascript - 如何在使用拆分后获取数组中的第一项

Javascript:检测窗口何时完全加载