-1

I want to implement onkeypress event for a textbox in asp.net to call a javascript function that validates textbox for number field. How can i validate a textbox in asp.net for number field using javascript function but that javascript function is not calling. This is my javascript function

function FDigit(x)
{
if (x.KeyCode < 48 || x.KeyCode > 57)
{
    alert("Enter Digits only");
    return false;
}

this is my textbox control from where i have called for onkeypress event.

<td><asp:TextBox ID="txtSalary" runat="server" OnKeyPress="return FDigit(x)"></asp:TextBox>
1

1 Answer 1

1

You can pass eventto to your function rather than x:

<asp:TextBox ID="txtSalary" runat="server" onkeypress="return FDigit(event);" />

In the function, use keyCode instead of KeyCode:

function FDigit(evt) {
    if (evt.keyCode < 48 || evt.keyCode > 57) {
        alert("Enter Digits only!");
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.