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

Posted by Iluvdotnetfunda under ASP.NET on 4/18/2012 | Points: 10 | Views : 105416 | Status : [Member] | Replies : 19
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: 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: 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

Login to post response