Link to home
Start Free TrialLog in
Avatar of asudhaa
asudhaa

asked on

Paste only Numeric value in textbox

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;
        }
   </script>
Avatar of Sara bhai
Sara bhai
Flag of India image

try this...

function isNumberKey(val)
    {
    if(isNaN(val)){
    val = val.substring(0, val.length-1);
    document.form1.txtAge.value = val;
   alert("Please enter a Numeric value");
    return false;
    }
    return true;
    }
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
handle the TextChanged event to validate your complete field as shown in http://emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
if(NaN(value)) ... tells whether the value is a number or not