Paste only Numeric value in textbox

Posted on 2011-05-04
Last Modified: 2012-05-11
I have created dynamic text boxes .I would like to enter only numeric values.It works fine when key is pressd.But when i copy and paste i am able to paste non integer values.
        how can I paste only numberic values and not non integer values?

tb.Attributes.Add("onkeypress", "return isNumberKey(event);");

the javascript code is
<script type="text/javascript">
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                alert("Please enter a Numeric value");
                return false;
             return true;
Question by:asudhaa
    LVL 9

    Expert Comment

    try this...

    function isNumberKey(val)
        val = val.substring(0, val.length-1);
        document.form1.txtAge.value = val;
       alert("Please enter a Numeric value");
        return false;
        return true;
    LVL 53

    Accepted Solution

    LVL 69

    Expert Comment

    by:Éric Moreau
    handle the TextChanged event to validate your complete field as shown in
    LVL 2

    Expert Comment

    if(NaN(value)) ... tells whether the value is a number or not

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now