How to check whether the textbox have only numbers.

The following code sample shows how to check whether the textbox have only numbers.
int a=0;
foreach(string s in textbox1.text)
{
a=s;
if ((s>65 && s<90) || (s>97 && s<122))
{
messagebox.show("Characters are not allowed");
}
}
//using the ascii values we checking whether the values present in the textbox is character or numbers.


Comments

Author: kingfisher12 May 2008 Member Level: Gold   Points : 2


CHNumeric = IsNumeric(txtbox1.Text)
If CHNumeric = False Then
MsgBox("Enter numeric value Only", MsgBoxStyle.Exclamation, Application.ProductName)
txtbox1.Focus()
Exit Sub
End If

'----------> I have used an inbuilt function "IsNumeric" which returns boolean value after checking for numeric values

Author: santhoshkumar23 May 2008 Member Level: Gold   Points : 2

By default the textbox value is string right, then how can v check isNumeric for string value.

Author: santhoshkumar04 Jun 2008 Member Level: Gold   Points : 0

ok. thank you ranipriya

Author: sivangari11 Jun 2008 Member Level: Gold   Points : 2


protected void Button1_Click(object sender, EventArgs e)
{
string str = TextBox1.Text;
string str1 = TextBox4.Text;

int result = 0;
if( (int.TryParse(str, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result)) && (int.TryParse(str1, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.CurrentInfo, out result)))
{
Label1.Text = "Number";
}
else
{
Label1.Text = "string";
}

Author: KR12 Dec 2008 Member Level: Gold   Points : 1

Hi,
Find the below code,

private void button1_Click(object sender, EventArgs e)
{
int s;
if (!int.TryParse(textBox1.Text, out s))
{
MessageBox.Show("Charactes not allowed");
}
}

Author: Abhinav Misra14 Dec 2008 Member Level: Silver   Points : 1

dont u think that it is better to use

java script or compare validation control and set its property
for type check

Author: Radhu07 Aug 2009 Member Level: Gold   Points : 2

//this will enable user to enter only numeric values

function EnableKeys()
{

var keyCode = event.keyCode;

//Enable Only numeric values

if(keyCode>=48 && keyCode<=57 )
{
event.returnValue = true;
}
else
{
event.returnValue = false;
}
}




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: