14,694,918 members
See more:
i m having text box value as test10

i want to get integer value i.e 10

i used code as
int b = Convert.ToInt32(Txt1.Text);
Response.Write(b);

<br />
it show error as "Input string was not in a correct format."

how to get the value please tell me
Posted

## Solution 10

Try this:
string Numbers = "";
for(int i = 0; i < Txt1.Length; i++)
{
if ((byte)Txt1.Text[i] >= 38 & (byte)Txt1.Text[i] <= 57)
{
Numbers = Numbers + "" + Txt1.Text[i];
}
}
Response.Write(Convert.ToInt32(Numbers));
v2
Kim Togo 2-May-11 12:26pm

Numbers in ASCII charset begins from 48 and ends in 57.
http://www.asciitable.com/

char 38 to 47 has nothing to do with numbers.

## Solution 9

All well and valid but he wants to inform user :)
int b;
try
{
b =Int32.Parse(Text);
}
catch (System.FormatException ex)
{
Response.Write("Integer Dude!!! I N T E G E R!");
}
catch (System.OverflowException)
{
Response.Write("Don't test me...");
}
finally
{
b = 0;
}
Ilirian Citaku 27-Aug-19 5:16am

ex is never used

## Solution 8

Hi beginner in C#.net,

You can use this function for validating the textBox input:
using System.Globalization;

public static bool IsNumeric(string value, System.Globalization.NumberStyles NumberStyle)
{
Double result;
return Double.TryParse(value, NumberStyle,System.Globalization.CultureInfo.CurrentCulture, out result);
}

Just call the function where value is the TextBox text and you can use NumberStyle.Integer for validating the text as integer value.

I hope this help,
:)
v2
RaviRanjanKr 2-May-11 10:18am

Kim Togo 2-May-11 10:39am

Nice method. But it will not solve OP question. Will the method work if the text is "test10" ?

## Solution 7

you need regex for this

//first you need to include header file

using System.Text.RegularExpressions;

string newString = Regex.Replace("test10", "[^.0-9]", "");
int k = int.Parse(newString);
Mahendra.p25 2-May-11 7:54am

string newString = Regex.Replace(Textbox1.Text, "[^.0-9]", "");
Kim Togo 2-May-11 9:34am

My 5.
Just use \d.
string newString = Regex.Replace(Textbox1.Text, @"[^\d]", string.Empty);
beginner in C#.net 2-May-11 7:55am

its working thank u sir.
my 5
Tarun.K.S 2-May-11 8:05am

Then accept the answer as well.
yesotaso 2-May-11 8:12am

How about "Regex.Replace("-3E7", "[^.0-9]", "");"? is it not valid?
Kim Togo 2-May-11 9:33am

Try to use string.Empty instead of "". It is easer to read and understand.

## Solution 5

Learn basic validation from net
u can use regularexpressionvalidator or javascript

http://onetidbit.wordpress.com/2008/01/25/14/[^]

## Solution 4

You could do one/all of these:

0) Handle the TextChanged event for the TextBox and simply not allow anything but integer values to be input

1) Wait until the user clicks the submit button and call Int32.TryParse() method instead of Convert.ToInt32().

2) Use the built-in asp.net validator functionality.

## Solution 3

You can use Regular Expression[^] to get the value or validate input.

string valueString = System.Text.RegularExpressions.Regex.Replace(Txt1.Text, @"[^\d]", string.Empty, System.Text.RegularExpressions.RegexOptions.Compiled);
int b = int.Parse(valueString);

This will get you these outputs:

"10tons wast and 8 tons of stone" => 108
"108" => 108
"Hello Peter, please call me at (0)47-784-9787-1045" => 04778497871045

You have to be more specific. What if you have a value of "10tons wast and 8 tons of stone".
Do you then what the integer value to be "108" or just "10" or "8" ?
v4

## Solution 2

just try!!

using System.Text.RegularExpressions;

string newString = Regex.Replace(Convert.Tostring(Txt1.Text), "[^.0-9]", "");
int b = Convert.ToInt32(newString);
Groulien 2-May-11 7:38am

Nice, way cleaner than mine.
5
RAJI @Codeproject 2-May-11 7:41am

Thank you... i got it becoz i tried it before
beginner in C#.net 2-May-11 8:09am

beginner in C#.net 2-May-11 8:22am

tnx for ur ans.. its working
Kim Togo 2-May-11 10:41am

Try to use string.Empty instead of "". It is easer to read and understand.
My 5. Good use of regex

## Solution 1

Your best bet is to check for each character if it is a number.
My code snippet;
Test123Test456 will become 123456
string Numbers = "";
for(int i = 0; i < Txt1.Length; i++) //Iterate through all the characters
{
if ((byte)Txt1.Text[i] >= 38 && (byte)Txt1.Text[i] <= 57) //Using ascii values to check if it's a number, see also; ASCII Table
{
Numbers += Txt1.Text[i]; //If it's a number, store it in Numbers
}
}
Response.Write(Convert.ToInt32(Numbers));
v2
Kim Togo 2-May-11 10:15am

Something is wrong in your if statement.
Numbers in ASCII charset begins from 48 and ends in 57.
http://www.asciitable.com/

char 38 to 47 has nothing to do with numbers.