Testing for non numeric data but must allow decimal point
I am trying to check for non numeric data however the user must be able to use decimal points, I have the following code so far but this does not allow decimal point, could someone advise how I would go about this please?
Thanks in advance.
Code:
// non numeric data check
int num1;
bool isNumeric1 = int.TryParse(txtHeight.Text, out num1);
if (isNumeric1)
{
}
else
{
txtHeight.Text = ("");
MessageBox.Show("Only enter numbers into\nthe Height text field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// non numeric data check
int num2;
bool isNumeric2 = int.TryParse(txtWeight.Text, out num2);
if (isNumeric2)
{
// convert and display conversion in appropriate textfield, call class to convert
}
else
{
txtWeight.Text = ("");
MessageBox.Show("Only enter numbers into\nthe Weight text field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Re: Testing for non numeric data but must allow decimal point
I would do it with RegEx. I think this should work.
But to be hones, I could not understand your question. You meant
(1)non-numerc data and/with decimal point should be allowed
(2)numeric data with decimal point should be allowed
Writing solution for both. May be true and false which is returned by the expression should be interchanged.
Code:
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Regex.IsMatch(yourString, @"\A(\D+)\Z"); //(1)
Regex.IsMatch(yourString, @"\A(\d+.{0,1})\Z");//(2)
}
}
}
Be careful about capital and small d in pattern.
Re: Testing for non numeric data but must allow decimal point
Are you saying that you want to support numbers like "5" as well as "5.5"? If so, then int.Parse is the wrong thing to use as it only parses valid *integers*. If you want to parse valid floating point numbers use double.Parse/float.Parse. You can control exactly what styles of floating point numbers are allowed by using the Parse overload which takes a "NumberStyles" parameter. Regex is complete overkill for such a simple task.
Re: Testing for non numeric data but must allow decimal point
Actually I'm fairly sure that winforms comes with a few built in validators which should do the task easily enough for you. http://blog.scosby.com/post/2010/02/...ows-Forms.aspx
Re: Testing for non numeric data but must allow decimal point
Thanks for the info, yes I want my conversions to allow whole numbers as well as decimal points but not allow the obvious such as letters in a number.
I will post back upon the issue being resolved.
Re: Testing for non numeric data but must allow decimal point
I have tried
Code:
double dOutput = 0;
if (Double.TryParse(txtHeight.Text, out dOutput))
{
MessageBox.Show("Valid double");
}
else
{
MessageBox.Show("Only enter numbers and decimal point where necessary");
}
but this does not catch non numeric data, am I missing the point?
Re: Testing for non numeric data but must allow decimal point
It should work. You should also give NumberStyle.AllowDecimalPoint (or something, dont remember what exactly it was)
double.Pasre(yourString, NumberStyleFormat.AllowDecimalPoint)
Catch exception for anything other that number
Re: Testing for non numeric data but must allow decimal point
Also check your culture settings
or
double.TryParse(yourString, NumberStyles.AllowDecimal, CultureInfo.InvariantCulture, out yourNumber);
Re: Testing for non numeric data but must allow decimal point
"but this does not catch non numeric data, am I missing the point? "
What doesn't it catch? If Double.TryParse returns true, it was a valid number and you're just mistaken.
Quote:
Also check your culture settings
or
double.TryParse(yourString, NumberStyles.AllowDecimal, CultureInfo.InvariantCulture, out yourNumber);
As long as your application is only ever run in a culture which uses a comma to separate thousands and a full stop for the fractional part, you won't have an issue with the InvariantCulture. As soon as you want to run your app in France, it'll crash and blow up. You should run on the assumption that user input will always be in the current culture. i.e. a french guy will input in the normal french way, an irish guy will input in the normal irish way.
Re: Testing for non numeric data but must allow decimal point
I am Programmer in Germany but I use English culture for most of my application. My system default is DE, so I have to be careful about it while programming. That was why I made this suggestion.