Hi,
I'm working on a c# wpf application in which I want to validate user input in a textbox. I have a validation rule like this:
public class NumericRule : ValidationRule
{
/// <summary>
/// Checks whether a value can be converted to a numeric value.
/// </summary>
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string input = value as string;
//try conversion
int result;
var status = int.TryParse(input, NumberStyles.Any,
CultureInfo.InvariantCulture, out result);
//return valid result if conversion succeeded
return status ? ValidationResult.ValidResult
: new ValidationResult(false, input + " is not a number");
}
}
However, when entering a number with decimals like 3.5, it says the number is invalid. When entering 3,5 it is ok. I would like both numbers to be valid and if a value with a comma is entered, it is converted to a dot. Also I would like to have a maximum of 3 decimals after the dot..
Any help?
Thanks