C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to check whether the textbox have only numbers.


Posted Date: 05 May 2008    Resource Type: Code Snippets    Category: Validations
Author: santhoshkumarMember Level: Gold    
Rating: Points: 10



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.




Responses

Author: kingfisher    12 May 2008Member Level: Silver   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: santhoshkumar    23 May 2008Member Level: Gold   Points : 2
By default the textbox value is string right, then how can v check isNumeric for string value.


Author: ranipriya    04 Jun 2008Member Level: Gold   Points : 2
Santhosh, No need of complex code use only simple code

Check this vb.net code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IsNumeric(t1.Text) Then
MsgBox("Enter numeric value", MsgBoxStyle.Critical)
Else
MsgBox("You entered " & t1.Text,
MsgBoxStyle.Information)
End If
End Sub


Function IsNumeric is there..

Public Function IsNumeric(ByVal Expression As Object) As Boolean
Member of: Microsoft.VisualBasic.Information

Summary:
Returns a Boolean value indicating whether an expression can be evaluated as a number.

OK....



Author: santhoshkumar    04 Jun 2008Member Level: Gold   Points : 0
ok. thank you ranipriya


Author: sivangari    11 Jun 2008Member 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: KR    12 Dec 2008Member 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 Misra    14 Dec 2008Member 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


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to check and see if a string is all uppercase in c#
Previous Resource: Allows one or more alphabetical and/or numeric characters. This is a more generic validation functi
Return to Discussion Resource Index
Post New Resource
Category: Validations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use