Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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);

it show error as "Input string was not in a correct format."

 
how to get the value please tell me
Posted 2-May-11 1:31am
Rate this: bad
good
Please Sign up or sign in to vote.

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);
 
  Permalink  
Comments
Mahendra.p25 at 2-May-11 7:54am
   
string newString = Regex.Replace(Textbox1.Text, "[^.0-9]", "");
Kim Togo at 2-May-11 9:34am
   
My 5.
Just use \d.
string newString = Regex.Replace(Textbox1.Text, @"[^\d]", string.Empty);
beginner in C#.net at 2-May-11 7:55am
   
its working thank u sir.
my 5
Tarun.K.S at 2-May-11 8:05am
   
Then accept the answer as well.
yesotaso at 2-May-11 8:12am
   
How about "Regex.Replace("-3E7", "[^.0-9]", "");"? is it not valid?
Kim Togo at 2-May-11 9:33am
   
Try to use string.Empty instead of "". It is easer to read and understand.
Rate this: bad
good
Please Sign up or sign in to vote.

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,
Smile | :)
  Permalink  
v2
Comments
RaviRanjankr at 2-May-11 10:18am
   
Nice Answer! My 5 :)
Kim Togo at 2-May-11 10:39am
   
Nice method. But it will not solve OP question. Will the method work if the text is "test10" ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

just try!!
 
using System.Text.RegularExpressions;
 
string newString = Regex.Replace(Convert.Tostring(Txt1.Text), "[^.0-9]", "");
int b = Convert.ToInt32(newString);
  Permalink  
Comments
nbgangsta at 2-May-11 7:38am
   
Nice, way cleaner than mine.
5
RAJI MOL S at 2-May-11 7:41am
   
Thank you... i got it becoz i tried it before
beginner in C#.net at 2-May-11 8:09am
   
maheindre gave answer.
beginner in C#.net at 2-May-11 8:22am
   
tnx for ur ans.. its working
Kim Togo at 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
Rate this: bad
good
Please Sign up or sign in to vote.

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));
  Permalink  
v2
Comments
Kim Togo at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

All well and valid but he wants to inform user Smile | :)
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;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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));
  Permalink  
v2
Comments
Kim Togo at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

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" ?
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

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.
 
Google is your friend.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Learn basic validation from net
u can use regularexpressionvalidator or javascript
various links are there like
 
http://onetidbit.wordpress.com/2008/01/25/14/[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 370
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Vinay Mistry 160
4 Sergey Alexandrovich Kryukov 130
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 24 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100