问题描述
我正在尝试使用 ASP.net 实现表单验证,并且尝试了所有建议的解决方案 此处 但最好的是 aspsnippets.com 到目前为止.
I'm trying to implement a form validation with ASP.net and I have tried every solution suggested here but the best one was on aspsnippets.com so far.
我的代码如下:
<asp:TextBox ID="tTitle" runat="server" onblur="WebForm_OnBlur()"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="tTitle"/>
<asp:TextBox ID="tEMail" runat="server" onblur="WebForm_OnBlur()"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="tEMail"/>
<asp:RegularExpressionValidator runat="server" ControlToValidate="tEMail"
ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"/>
<asp:LinkButton ID="btnSubmit" runat="server" Text="Submit"/>
Javascript
<script type="text/javascript">
function WebForm_OnSubmit() {
if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
{
for (var i in Page_Validators) {
try {
var control =
document.getElementById(Page_Validators[i].controltovalidate);
if (!Page_Validators[i].isvalid) {
control.className = "error";
} else {
control.className = "";
}
} catch (e) { }
} return false;
} return true;
}
function WebForm_OnBlur() {
for (var i in Page_Validators) {
try {
var control =
document.getElementById(Page_Validators[i].controltovalidate);
if (!Page_Validators[i].isvalid) {
control.className = "error";
} else {
control.className = "";
}
} catch (e) { }
} return false;
}
</script>
问题是电子邮件字段仅验证正则表达式.如果我更改验证器的顺序,它只会验证所需的表达式.
The problem is the e-mail field only validates for the regular expression. If I change the order of the validators, it only validates for required expression.
可能的问题是代码循环遍历所有验证器,但不会一次比较引用相同控件的验证器.这会导致只对控件应用最后一个验证器条件.
The possible problem is that the code loops through all the validators but does not compare the ones that reference the same control at once. This causes only the last validator condition to be applied on the control.
推荐答案
可能的问题是代码循环遍历所有验证器,但不会一次比较引用相同控件的验证器.这会导致只对控件应用最后一个验证器条件.
The possible problem is that the code loops through all the validators but does not compare the ones that reference the same control at once. This causes only the last validator condition to be applied on the control.
是的,这确实是问题所在.要修复它,您可以执行以下操作:
Yes, this is indeed the problem. To fix it, you can do the following:
在 WebForm_OnBlur
函数中,循环遍历与失去焦点的控件关联的验证器(而不是页面上的所有验证器),并仅清除 className
属性如果所有验证器都有效:
In the WebForm_OnBlur
function, loop through the validators associated with the control that lost focus (rather than all the validators on the page), and clear the className
property only if all the validators are valid:
function WebForm_OnBlur(control) {
for (var i = 0; i < control.Validators.length; i++) {
if (!control.Validators[i].isvalid) {
control.className = "error";
return;
}
}
control.className = "";
}
在TextBox
控件的onblur
属性中,将this
作为参数传递给WebForm_OnBlur
:>
In the onblur
attribute of the TextBox
controls, pass this
as the argument to WebForm_OnBlur
:
<asp:TextBox ID="tTitle" runat="server" onblur="WebForm_OnBlur(this)"/>
<asp:TextBox ID="tEMail" runat="server" onblur="WebForm_OnBlur(this)"/>
在WebForm_OnSubmit
函数中,为每个具有关联验证器的控件调用WebForm_OnBlur
:
In the WebForm_OnSubmit
function, call WebForm_OnBlur
for each control that has associated validators:
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) === "function" && ValidatorOnSubmit() === false) {
for (var i = 0; i < Page_Validators.length; i++) {
var control = document.getElementById(Page_Validators[i].controltovalidate);
if (Page_Validators[i] === control.Validators[0]) // minor optimization
WebForm_OnBlur(control);
}
return false;
}
return true;
}
这篇关于链接文本框控件的必填字段和正则表达式验证器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!