So I have a little method that validates a number input based on the users browser culture. This is in asp.net webforms.
public static bool IsNumberInCulture(object source, EventArgs e) {
TextBox validateSource = (TextBox)source;
string validateThis = validateSource.Text.Trim();
decimal result;
if (validateThis.Length > 0) {
if (decimal.TryParse(validateThis, NumberStyles.Number, UserCulture, out result)) {
if (validateThis != result.ToString("N0", UserCulture) && validateThis != result.ToString("N1", UserCulture) && validateThis != result.ToString("N2", UserCulture)) {
return false;
} else {
return true;
}
}
}
return false;
}
This appears to work really well. Except with Norwegian numbers.
What is strange, is that the browser formats the number display correctly. Example. 6 000,00
.
If I delete and replace the numbers individually in the text box, the number validates to true.
If I remove the number in the text box completely, and input a new number, using the same format (number, space, number, number, number, comma, number number) the number validates to false!!
I don't understand this. Anyone have any ideas?