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.
Some of my previous articles are as follows:
Code To Creat Google Colum Chart Using C#.Net and JavaScript in Asp.Net MVC | Google Colum Chart In Asp.Net MVC, Login Form With LightbBox Effect in Asp.Net, Allow Only Alphabets in TextBox Using Javascript in Asp.Net, JavaScript Confirm Message From Code Behind in Asp.Net Using C#, Display Alert Message on Page Load in MVC Application Using Javasscript, Email Address Validation in Javascript in Asp.Net.
Code To Creat Google Colum Chart Using C#.Net and JavaScript in Asp.Net MVC | Google Colum Chart In Asp.Net MVC, Login Form With LightbBox Effect in Asp.Net, Allow Only Alphabets in TextBox Using Javascript in Asp.Net, JavaScript Confirm Message From Code Behind in Asp.Net Using C#, Display Alert Message on Page Load in MVC Application Using Javasscript, Email Address Validation in Javascript in Asp.Net.
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
thanks very good
ReplyDelete