ASP.NET required field validator firing on focus in Firefox
Posted
by ren33
on Stack Overflow
See other posts from Stack Overflow
or by ren33
Published on 2010-05-20T15:54:08Z
Indexed on
2010/05/20
16:40 UTC
Read the original article
Hit count: 463
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. This is only happening in Firefox. How can I prevent the validator from firing when I first enter the textbox?
edit
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.
© Stack Overflow or respective owner