.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Steve Jackman
Imran Ghani
Santhakumar Munuswamy

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Allow numbers only in textbox window application

Posted By: Ramesh     Posted Date: March 29, 2010    Points:2   Category :Windows Application
In windows application i have a textbox in that only number is allowed. If user enter any alphabetical is entered by the user than it should alert user or not allowed only.

I am using windows application and C# as my code language

Regards
Ramesh

Responses
Author: Lalij Mer             
Posted Date: March 30, 2010     Points: 5   

Hi,Ramesh...
You can try this code sample



private void Workinghours_TextBox_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false & char.IsControl(e.KeyChar) == false) {
e.Handled = true;
MessageBox.Show("Please enter valid number", "Boat House", MessageBoxButtons.OK);
}

}
OR


<HTML>
<HEAD>
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
</BODY>
</HTML>

show here more reference will help

http://www.c-sharpcorner.com/UploadFile/tadas/NumericTextBox12062005220207PM/NumericTextBox.aspx

Please Check my answer if you help....
Thank You...

Lalit.
Author: Dilip             
Posted Date: March 30, 2010     Points: 5   

This regex can validate integers and optional floating point numbers:

[0-9]*.?[0-9]+

And instead of using regular expressions, you could use isNaN(value) to check if the numerical value is not a number (NaN).

Regards
Dilip
Author: Narayanan             
Posted Date: January 17, 2012     Points: 20   

Hi,
Try this, This is a Code
   private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
base.OnKeyPress(e);
}


Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend