Tuesday 15 May 2012

Numeric Textbox in ASP.NET 4.0


Numberic TextBox allows Only Numbers,for others, message will be displayed.

step 1)  add textbox to asp.net page
step 2) add javascript code which will process onkeypress event,
           in keypress, get elemnt by id in this case TextBox3, check for numeric key

<body>
    <form id="form1" runat="server">
    <div>
         <asp:TextBox ID="TextBox3" runat="server"  ></asp:TextBox>
    </div>

      <script type="text/javascript">
       document.getElementById("TextBox3").onkeypress = function (e) {
           var ret = false;
           var e = window.event || e
           var keyunicode = e.charCode || e.keyCode
           if (keyunicode >= 48 && keyunicode <= 57) ret = true;
           else alert('Only Numbers allowed');


           return ret;
       }
</script>

    </form>
</body>

No comments:

Post a Comment