Click here to Skip to main content
14,740,319 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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);

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

how to get the value please tell me

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 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.
Hi beginner in,

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,
RaviRanjanKr 2-May-11 10:18am
Nice Answer! My 5 :)
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" ?
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.
RAJI @Codeproject 2-May-11 7:41am
Thank you... i got it becoz i tried it before
beginner in 2-May-11 8:09am
maheindre gave answer.
beginner in 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
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
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.

char 38 to 47 has nothing to do with numbers.
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" ?
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 validator functionality.

Google is your friend.
Learn basic validation from net
u can use regularexpressionvalidator or javascript
various links are there like[^]
All well and valid but he wants to inform user :)
int b;
    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...");
    b = 0;
Ilirian Citaku 27-Aug-19 5:16am
ex is never used
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]; 
Kim Togo 2-May-11 12:26pm
Numbers in ASCII charset begins from 48 and ends in 57.

char 38 to 47 has nothing to do with numbers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900