IsNumeric validation Compact Framework 2.0

  • Thread starter Thread starter Malani Czornon
  • Start date Start date
M

Malani Czornon

Hi, are there any function in C# to validate that the value from a textbox
to be numeric ?

Thanks in advance.
 
Depending on your needs, most of the Parse functions would work:

int myval = int.Parse(textBox.Text);
float myval = float.Parse(textBox.Text);

etc.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
Thanks Chirs for your reply, but Parse doesn´t return true or false.... it
returns an expection....
I need to get a bool value...
 
I see. That's way more complex then.

public bool IsInt(string value)
{
try { int.Parse(value); }
catch { return false; }
return true;
}

Close to quantum physics.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
Chris,

Beware of quantum physics. This stuff is way too unstable to be used in a
beginner's program: Catching the exception is somehow measuring it, which
would bring Heisenberg and his uncertainty principle into the game. From
there on, false would no longer be sure whether he's actually as false as
that, which would probably lead to more exceptions being thrown randomly.
You'd probably need to include some statistical physics library into your
program to analyze the spectrum of these exceptions. Monte Carlo would
surely help. And that's where it hurts: Do Monte Carlo implementations
internally use bool results? Better not because they'd start colliding into
these other bools they are evaluating, which might actually lead to a bool
fission reaction. Which by globally destroying the whole binary system would
somehow solve the OP's concern the hard way (lack of any form of computer, I
mean). And all we wanted to do is to know if a string can be converted to an
int. I warned you: Unstable stuff, this quantum physics.

BTW, do you think this could be considered experimental evidence of the
butterfly effect? Someone should give it a try some day.

Serge.
http://www.apptranslator.com
 
Hi, are there any function in C# to validate that the value from a textbox
to be numeric ?

Thanks in advance.

Have you tried int.tryParse

int i = -1;
bool isNumeric = int.tryParse(textBox1.Text, out i);

If isNumeric is true then i holds numeric value.
 
No TryParse in CF :-(

HTH,

Serge.
http://www.apptranslator.com - Localization tool for your applications


Hi, are there any function in C# to validate that the value from a textbox
to be numeric ?

Thanks in advance.

Have you tried int.tryParse

int i = -1;
bool isNumeric = int.tryParse(textBox1.Text, out i);

If isNumeric is true then i holds numeric value.
 
Hi,

Maybe creating your own control which inherits from TextBox and only allows
to input a number value or digit?

Dav
 
public static bool isPosInteger(string strInput)
{
Regex rx = new Regex(@"^\d+$");
if (rx.IsMatch(strInput))
{
return true;
}
else
{
return false;
}
}

public static bool isNumeric(string strInput)
{
Regex rx = new
Regex(@"([+-][0-9]+\.[0-9]*)|([0-9]*\.[0-9]+)|([0-9]+)");
if (rx.IsMatch(strInput))
{
return true;
}
else
{
return false;
}
}

public static bool isPosNumber(string strInput, bool required, int
numDecimals)
{
if (required)
{
if (strInput == "" || strInput == null)
{
return false;
}
}
else
{
if (strInput == "" || strInput == null)
{
return true;
}
}
Regex rx = new Regex(@"^(?=.*[0-9].*$)\d{0,}(?:\.\d{0," + numDecimals
+ "})?$");
if (rx.IsMatch(strInput))
{
return true;
}
else
{
return false;
}
}

Please check I have created functions, which one is helpful to you.

Regards,
Mayank Parmar
http://developereye.blogspot.com/
 
Back
Top