views:

98

answers:

1

I want to validate the value a user enters in a text box, so that it only enters float numbers. I'm not interested in range. How can I do this, considering also culture localization information (e.g. "." or "," as separators)?

+1  A: 

My usual method is to use a RegexValidator with a validation expression of ^(\d+(\.\d*)?)|(\d*(\.\d+))$. You could ammend this to enable "." or ",": ^(\d+([\.,]\d*)?)|(\d*([\.,]\d+))$.

If you wanted to be strictly correct, you'd enable the correct validation expression for each culture.

Also note that you still need a RequiredFieldValidator if the value is compulsary.

David Kemp
I'm not normally a fan of using a regex to test whether something is a number, but I'll concede that it has the nice advantage in this case of doing the same validation both client and server-side while only writing the logic once.
Joel Coehoorn