Disallow letters in a textbox...

Hi,

How do you disallow the textbox for accepting letters?

I do not want to use regular expression since it is validated only when the control losses focus.

I want it to happen during text keypress....

is this possible...

I am using ASP.net 1.1.

Regards,

Joseph
LVL 15
JackOfPHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas413Commented:
do following

<asp:TextBox id="txtQty"  runat="server" onkeypress= "fncInputNumericValuesOnly('txtQty')"/>

write JavaScript function something like given below in your aspx page

function fncInputNumericValuesOnly()
      {
            if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
            {
                  event.returnValue=false;
            }
      }

this function will allow only numeric characters in text box.. you can change the values according to your requirement
0
JackOfPHAuthor Commented:
It does not work...

any Ideas why? Below is my code...
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript" >
    function fncInputNumericValuesOnly()
      {
            if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
            {
                  event.returnValue=false;
            }
      }
      
      </script>

</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeypress="fncInputNumericValuesOnly(TextBox1)"></asp:TextBox>
     </div>
    </form>
</body>
</html>

Open in new window

0
JackOfPHAuthor Commented:
Is there something I need to set?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

vikas413Commented:
Try this
      <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript" >
    function fncInputNumericValuesOnly()
      {
            if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
            {
                  event.returnValue=false;
            }
      }
      
      </script>

</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeypress="fncInputNumericValuesOnly()"></asp:TextBox>
     </div>
    </form>
</body>
</html>

Open in new window

0
masterpassCommented:
Have a look at these
ASPX
-------
<asp:TextBox ID="NumberTextBox" runat="server" onKeyPress="return CheckNumericKeyInfo(event.keyCode, event.which);" />

JS
-----
function CheckNumericKeyInfo($char, $mozChar) {
    if ($mozChar != null) { // Look for a Mozilla-compatible browser
        if (($mozChar >= 48 && $mozChar <= 57) || $mozChar == 0 || $char == 8 || $mozChar == 13) $RetVal = true;
        else $RetVal = false;
    }
    else { // Must be an IE-compatible Browser
        if (($char >= 48 && $char <= 57) || $char == 13) $RetVal = true;
        else $RetVal = false;
    }
    return $RetVal;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vikas413Commented:
here is equivalent HTML page you can convert it to ASPX by replacing textbox input to aspx text box control

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript" >
    function fncInputNumericValuesOnly()
      {
            if(!(event.keyCode==45||event.keyCode==46||event.keyCode==48||event.keyCode==49||event.keyCode==50||event.keyCode==51||event.keyCode==52||event.keyCode==53||event.keyCode==54||event.keyCode==55||event.keyCode==56||event.keyCode==57))
            {
                  event.returnValue=false;
            }
      }
      
      </script>

</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <input type="Text" ID="TextBox1"  onkeypress="fncInputNumericValuesOnly()"/>
     </div>
    </form>
</body>
</html>

Open in new window

0
JackOfPHAuthor Commented:
I am using firefox, did you try the code in firefox?

I donot know why it is not working...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.