1 February 2014

How To Restrict TextBox To Only Accept Numeric Values in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UI.WebForm1" %>

<!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 type="text/javascript">


        function isNumber(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                alert("Please Enter Only Numeric Value..");
                return false;
            }

            return true;
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <asp:TextBox ID="txt_number" onkeypress="return isNumber(event)" runat="server"></asp:TextBox>
        </center>
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment