Accept only numeric values in TextBox using JavaScript


In this article, we have make textbox that take only numeric values using java script.
Normally we need to use this code for cost, price and salary


Design


Source Code


<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="numerictextbox.aspx.cs" Inherits="numerictextbox" %>



<!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>
    <title>number system </title>
    <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8); //Backspace
        function IsNumeric(e) {
            var keyCode = e.which ? e.which : e.keyCode
            var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            document.getElementById("error").style.display = ret ? "none" : "inline";
            return ret;
        }
    </script>
</head>
<body>
    <fieldset style="width: 210px;">
        <legend>Numeric Value for this textbox:</legend>
        <input type="text" id="numericvalue" onkeypress="return IsNumeric(event);" ondrop="return false;"


            onpaste="return false;" />
            <br />
        <span id="error" style="color: Red; display: none">Please enter only numeric value
            between 0 to 9</span>
    </fieldset>
</body>
</html>


Out Put:-



Accept only numeric values in TextBox using JavaScript Accept only numeric values in TextBox using JavaScript Reviewed by NEERAJ SRIVASTAVA on 3:32:00 PM Rating: 5

No comments:

Powered by Blogger.