Enter International Numbers in a Textbox

This is a very common case…. There are so many application that needs to enter numbers in a text box… but what is the best method to do that.

First, you need to validate the number as the user enters it in the text box:

Depending on your required type you can use the .TryParse to check the validity of the input.

This is a small code sample,

Double.TryParse(textBox1.Text,NumberStyles.Any,CultureInfo.CurrentCulture,out value)

This function returns a bool, test your bool and if cann’t be parsed then raise a validation error or disable the user from proceeding.

The default is to use CultureInfo.CurrentCulture, so, when you use the sample on machine with English or Arabic local, it would accept the thousand separator (,)… when you work on a machine with French locale it wouldn’t accept the thousand separator (,).

In the case you need to specify a different formatting of the numbers… for example, accept only number formatting according to the French (France locale). Then you can specify the correct culture.

For example,

Double.TryParse(textBox1.Text,NumberStyles.Any, new CultureInfo(fr-FR”),out value)

This way you can verify the correct the number formatting before you accept the numbers in a textbox.