How to allow only Numeric values into a textbox? [Resolved]

Posted by Iluvdotnetfunda under ASP.NET on 4/18/2012 | Points: 10 | Views : 96567 | Status : [Member] | Replies : 24
Hi Friends,

How to allow only Numeric values into a asp:textbox.

Thanks
iluvdotnetfunda




Responses

Posted by: Gsm_Gsv on: 4/18/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Markup:
<input type="text" id="yourId" name="onlynum" onkeypress="return isNumericKey(event);" />

Javascript:
function isNumericKey(e)

{
var charInp = window.event.keyCode;
if (charInp > 31 && (charInp < 48 || charInp > 57))
{
return false;
}
return true;
}


---------------------------------------
Live the life you've dreamed

Regards
MADHU

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

The below java script function will helps you to allow only Integer value.
function NumberOnly() {
var AsciiValue = event.keyCode
if ((AsciiValue >= 48 && AsciiValue <= 57) || (AsciiValue == 8 || AsciiValue == 127))
event.returnValue = true;
else
event.returnValue = false;
}

<input type="text" id="txt1" onkeypress="return NumberOnly()"/>



Thanks & Rgards,
Dhiren Kumar Kaunar

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/18/2012 [Member] Silver | Points: 25

Up
0
Down
write textChanged event...

        Dim i As Integer

Dim flag As Boolean = True
For i = 0 To TextBox1.Text.Length - 1
If IsNumeric(TextBox1.Text.Chars(i)) Then
flag = False
TextBox1.Text = TextBox1.Text.Remove(i)
MsgBox("Enter Text Only", vbCritical)
Exit For
End If
Next



Regards,
Singaravel M

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/18/2012 [Member] Silver | Points: 25

Up
0
Down
oh sorry..

asp code as follows..

<asp:TextBox id="txtNumbersOnly" Runat="server" />

<asp:RegularExpressionValidator Runat="server" ID="valNumbersOnly" ControlToValidate="txtNumbersOnly" Display="Dynamic" ErrorMessage="Please enter a numbers only in text box." ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)">
</asp:RegularExpressionValidator>



Regards,
Singaravel M

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/18/2012 [Member] Silver | Points: 25

Up
0
Down
See this link for asp:JScript..

http://www.codeproject.com/Tips/328178/Allow-only-Numeric-values-in-ASP-Text-box-control

Regards,
Singaravel M

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Write a regular expression in javascript

var regEx = ^/(0-9)/$;


if (!regEx.test(textboxid))
{
//error message;
}




------------------------------------------------
Learn throughout life

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Sakthi.Singaravel,

Its not working, by using this also it is allowing alphabet values.

This is my code

 <asp:TextBox runat="server" ID="textbox13" MaxLength="50" />

<asp:RegularExpressionValidator ID="rev1" runat="server" ControlToValidate="textbox13" ErrorMessage="Enter a valid number" ForeColor="Red" ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)" Display="Dynamic"></asp:RegularExpressionValidator>


Plzz check this and let me know.

Thanks

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gsm_Gsv,

without javascript that is not possible to do that?

Thanks

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Please check the below code .

<asp:TextBox ID="TextBox1" runat="server" />
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck"
Type="Integer"
ControlToValidate="TextBox1"
Text="Text must be an integer." />

Thanks & Rgards,
Dhiren Kumar Kaunar

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hello Dhiren.Kaunar@Gmail.Com,

This is not working .
Is this working for you.

Thanks

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsm_Gsv on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, Iluvdotnetfunda
possible, but better solution is by using javascript. Always perform client side validation using JavaScript to avoid unnecessary requests to server.


-------
Regards
MADHU

---------------------------------------
Live the life you've dreamed

Regards
MADHU

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gsm_Gsv,

Thanks for your great explanation. My problem is solved .

thanks

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hello Dhiren.Kaunar@Gmail.Com,

This is working fine.
Thanks for your help.Thank you so much.

iluvdotnetfunda

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Yes this code is working for me this is a javascript code only ..
Check the below complete code which I have written
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function NumberOnly() {
var AsciiValue = event.keyCode
if ((AsciiValue >= 48 && AsciiValue <= 57) || (AsciiValue == 8 || AsciiValue == 127))
event.returnValue = true;
else
event.returnValue = false;
}
</script>
</head>
<body>
Text Box 1
<input type="text" id="txt1" onkeypress="return NumberOnly()"/>
</body>
</html>

Thanks & Rgards,
Dhiren Kumar Kaunar

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Welcome .. Iluvdotnetfunda

Thanks & Rgards,
Dhiren Kumar Kaunar

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Dhiren.Kaunar@Gmail.Com ,

How to show Error Message for this.

Thanks
iluvdotnetfunda

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gsm_Gsv,

how to show error message for this javascript function.

Thanks
iluvdotnetfunda

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsm_Gsv on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Iluvdotnetfunda

That JavaScript function disables alphabetical keys.
If you cannot type alphabets for that particular field, what is the need of error message?

---------------------------------------
Live the life you've dreamed

Regards
MADHU

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gsm_Gsv,

To show the field as Mandatory

Thanks


Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
if you want to show the error message then change the code like below one
function NumberOnly() {
var AsciiValue = event.keyCode
if ((AsciiValue >= 48 && AsciiValue <= 57) || (AsciiValue == 8 || AsciiValue == 127))
event.returnValue = true;
else
{
alert ("Please enter numeric value");
//event.returnValue = false;
}
}

Thanks & Rgards,
Dhiren Kumar Kaunar

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Iluvdotnetfunda on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Gsm_Gsv,

Now its fine.

Thanks'
iluvdotnetfunda

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/18/2012 [Member] Silver | Points: 25

Up
0
Down
@ Dhiren.Kaunar@Gmail.Com

thank u..

Regards,
Singaravel M

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saimadhu84 on: 9/14/2012 [Member] Starter | Points: 25

Up
0
Down


hey I found the Correct solution here by using Regular expression

http://microsoftdotnetsolutions.blogspot.in/2012/09/use-this-regular-expression-to-allow.html

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/17/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Method-1
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Enter_only_numbers_in_textbox._Default" %>
<!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>Untitled Page</title>
<script type="text/javascript" language="javascript">
function numeric(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if(charCode > 31 && ((charCode >= 48 && charCode <= 57) || charCode == 46))
return true;
else
{
alert('Please Enter Numeric values.');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return numeric(event)"></asp:TextBox>
</div>
</form>
</body>
</html>

Method-2
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Enter_only_numbers_in_textbox.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>Untitled Page</title>
<script type="text/javascript" language="javascript">
function numeric( e )
{
var unicode = e.charCode ? e.charCode : e.keyCode;
if( unicode == 8 || unicode == 9 || ( unicode >= 48 && unicode <= 57 ) )
{
return true;
}
else
{
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" name="textbox1" id="textbox1" onkeypress="return numeric(event);" />
</div>
</form>
</body>
</html>




If this post helps you mark it as answer
Thanks

Iluvdotnetfunda, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response