ASP.NET 需要字段验证器在 Firefox 中触发焦点
我的更新面板中有 2 个 asp.net 文本框。两个文本框控件都具有一些附加到自动制表符到下一个字段的 JavaScript,并且仅允许数字输入。当我在第一个字段中输入一些数据并按 Enter 键时,焦点会转移到下一个字段,并且第二个字段的 requiredfieldvalidator 显示其“* required”错误消息,即使我刚刚输入该字段也是如此。当我第一次输入文本框时,如何防止验证器触发?
我还应该提到这两个文本框都位于 gridview 页脚中。
代码如下:
<asp:TextBox ID="add_ISBN" runat="server" Columns="14" MaxLength="17" CssClass="focus" />
<asp:TextBox ID="add_Qty" runat="server" Columns="4" MaxLength="4" />
<asp:RequiredFieldValidator ID="rfvQty" ControlToValidate="add_Qty" ErrorMessage="* required" ForeColor="Red" Display="Dynamic" EnableClientScript="true" ValidationGroup="Add" runat="server" />
在代码隐藏中:
add_ISBN.Attributes.Add("onkeydown", "return isbnCheck(event, '" & add_Qty.ClientID & "')")
和 javascript:
function isbnCheck(e, id) {
e = e || window.event;
var key = e.which || e.keyCode
if (validIsbnChars.indexOf(parseInt(key, 10)) >= 0) {
return true;
} else {
if (key == 13) {
var nextfield = document.getElementById(id);
if (nextfield) nextfield.focus();
return false;
}
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
return false;
}
}
javascript 仅允许有效的字符子集,如果用户按 Enter 键,则将焦点设置到下一个字段。
I have 2 asp.net textboxes in an update panel. Both textbox controls have some javascript attached to autotab to the next field and to allow only numeric input. When I enter some data into the first field and press enter, focus shifts to the next field and the requiredfieldvalidator of the second field displays its "* required" error message, even though I've just entered the field. How can I prevent the validator from firing when I first enter the textbox?
I should also mention that both textboxes are in a gridview footer.
Here's the code:
<asp:TextBox ID="add_ISBN" runat="server" Columns="14" MaxLength="17" CssClass="focus" />
<asp:TextBox ID="add_Qty" runat="server" Columns="4" MaxLength="4" />
<asp:RequiredFieldValidator ID="rfvQty" ControlToValidate="add_Qty" ErrorMessage="* required" ForeColor="Red" Display="Dynamic" EnableClientScript="true" ValidationGroup="Add" runat="server" />
In the codebehind:
add_ISBN.Attributes.Add("onkeydown", "return isbnCheck(event, '" & add_Qty.ClientID & "')")
And the javascript:
function isbnCheck(e, id) {
e = e || window.event;
var key = e.which || e.keyCode
if (validIsbnChars.indexOf(parseInt(key, 10)) >= 0) {
return true;
} else {
if (key == 13) {
var nextfield = document.getElementById(id);
if (nextfield) nextfield.focus();
return false;
}
if (e.preventDefault) e.preventDefault();
e.returnValue = false;
return false;
}
}
The javascript allows only a valid subset of characters, and if the user presses enter, sets focus to the next field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么会发生该错误,但您可以使用 客户端验证迷你 API。
在页面上查找执行
document.getElementById()
的验证器元素,然后在您按 Tab 键进入字段时将验证器上的isvalid
标志设置为 true。或者,您可以在现场时调用 ValidatorEnable(validatorElement, false) 将其禁用,然后在现场时使用 ValidatorEnable(validatorElement, true) 重新启用它选项卡退出。
I'm not sure why the bug is happening, but you can probably create a workaround using the client-side validation mini-api.
Find the validator element on the page doing a
document.getElementById()
, and then set theisvalid
flag to true on the validator when you tab into the field.Alternately you could call
ValidatorEnable(validatorElement, false)
to disable it while you're in the field, and then re-enable it withValidatorEnable(validatorElement, true)
when you tab out.我设法通过将 javascript 附加到 onKeyPress 事件而不是 onKeydown 来解决此问题。
I managed to fix this by attaching the javascript to the onKeyPress event instead of onKeydown.