Javascript 通过javascript重置asp.net验证控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2915830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset an asp.net validation control via javascript?
提问by Michael Kniskern
How do I reset an asp.net validation control via JavaScript? The current code sample clears the error message text but does not reset the validation control for the next form submission.
如何通过 JavaScript 重置 asp.net 验证控件?当前代码示例会清除错误消息文本,但不会为下一次表单提交重置验证控件。
var cv= document.getElementById("<%= MyValidationContorl.ClientID %>");
cv.innerHTML = '';
Update:
更新:
Here is the full code sample of the form. I can not seem to get the validation controls fire off on another form submission:
这是表单的完整代码示例。我似乎无法在另一个表单提交上触发验证控件:
function ClearData() {
var cv = document.getElementById("<%= MyValidationContorl.ClientID %>");
cv.innerHTML = '';
}
<html>
<form>
<asp:TextBox id="MyTextControl" runat="server" />
<asp:CustomValidator ID="MyValidationContorl" runat="server" />
<input type="button" onclick="javascript:ClearCCData(); return false;" runat="server" />
</form>
</html>
回答by MK.
Page validation is fired every time you do a post, what appears to be the problem is that you are clearing the validator content cv.innerHTML = '';, this way your validator message is lost forever and you'll think validation is not firing again.
每次发布帖子时都会触发页面验证,似乎问题在于您正在清除验证器内容cv.innerHTML = '';,这样您的验证器消息将永远丢失,您会认为验证不会再次触发。
and for @Glennular answer, the code does not handle the validator Displayproperty, if its set to Dynamicthe validator will be toggled using validator.style.display, but if its set to Noneor Inlinethen validator.style.visibilityproperty will be used instead.
对于@Glennular 答案,代码不处理验证器Display属性,如果将其设置为Dynamic验证器将使用 切换validator.style.display,但如果将其设置为None或Inline则将使用validator.style.visibility属性。
Its better to use asp.net ValidatorUpdateDisplayinstead,
最好改用asp.net ValidatorUpdateDisplay,
<script type="text/javascript">
function Page_ClientValidateReset() {
if (typeof (Page_Validators) != "undefined") {
for (var i = 0; i < Page_Validators.length; i++) {
var validator = Page_Validators[i];
validator.isvalid = true;
ValidatorUpdateDisplay(validator);
}
}
}
</script>
Update: Reset Validation Summaries
更新:重置验证摘要
<script type="text/javascript">
function Page_ValidationSummariesReset(){
if (typeof(Page_ValidationSummaries) == "undefined")
return;
for (var i = 0; i < Page_ValidationSummaries.length; i++)
Page_ValidationSummaries[i].style.display = "none";
}
</script>
回答by kerem
This one resets all validators in all validation groups.
这将重置所有验证组中的所有验证器。
<script type="text/javascript">
Page_ClientValidate('');
</script>
回答by Shararti
Try the following chunk of code :
尝试以下代码块:
$("#<%= txtUserSettingsEmailRequiredValidator.ClientID %>").css("display", "none");
I hope this will work as it worked for me. :)
我希望这对我有用。:)
回答by Glennular
Here's code to reset all validators
这是重置所有验证器的代码
function CleanForm() {
document.forms[0].reset();
for (i = 0; i < Page_Validators.length; i++) {
Page_Validators[i].style.visibility = 'hidden';
}
return false;
}
or a single one:
或单个:
document.getElementById("<%= MyValidationContorl.ClientID %>").style.visibility
= 'hidden';
回答by Lynn Dang
Using the Page_Validators[i].style.visibility = 'hidden';Don't work for me so I use this line of code instead: Page_Validators[i].enabled = false;
使用Page_Validators[i].style.visibility = 'hidden';Don't work for me 所以我改用这行代码:Page_Validators[i].enabled = false;
if (sFirstName == "" && sLastName == "")
{
alert('Reminder: Please first enter student ID to search for the student information before filling out the rest of the form field values');
//Disable all require field validation coontrol on the form so the user could continue to use the Lookup student function.
document.forms[0].reset();
for (i = 0; i < Page_Validators.length; i++) {
//Page_Validators[i].style.visibility = 'hidden';
Page_Validators[i].enabled = false;
}
return false;
}
else
{
alert('Student Name = ' + sFirstName + ' ' + sLastName);
document.forms[0].reset();
for (i = 0; i < Page_Validators.length; i++) {
//Page_Validators[i].style.visibility = 'hidden';
Page_Validators[i].enabled = true;
}
return true;
}

