Tuesday, 8 July 2014

How to Restrict User To Enter Only Numbers in Textbox Using JavaScript In Asp.Net

7/08/2014 - By Pranav Singh 1


In this article I will show how you can restrict user to enter only number and decimal number in a asp.net textbox using JavaScript.  Validation of entering only number in textbox will show in this article in asp.net. We will restrict user to add alphabet or special character or non numeric value in textbox.


So for this article we will create a new asp.net application. In this article we will add a page and in this page we will add an asp.net textbox control. Now we will add the JavaScript code for validating to enter only number in textbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Restrict to Enter only number.aspx.cs" Inherits="ProjectDemo_Asp.et.Restrict_to_Enter_only_number" %>

<!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>Restrict User To Enter Only Numbers in Textbox Using JavaScript In Asp.Net</title>
    <script type="text/javascript">
        function ValidateNmber(obj, evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            /*First decimal allowed*/
            if (charCode == 46 && isDecimal) {
                isDecimal = false;
                return true;
            }
            /*Dont allow more*/
            else if (charCode == 46 && !isDecimal) {
                return false;
            }
            if (charCode > 31 && (charCode < 48 || charCode > 57) || !isSubmit) {
                return false;
            }
            else {
                isDecimal = true;
                return true;
            }
        }
   </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      Enter Number: <asp:TextBox ID="TextBox1" runat="server"  onkeypress = "javascript:return ValidateNmber(this,event);"></asp:TextBox>
  
    </div>
    </form>
</body>
</html>

The above mention JavaScript code is used for restricting user to enter alphabet or any other special character in the textbox. Now run the page to view the output.

OUTPUT 1: Enter number 




OUTPUT 2: Enter decimal value

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top