regular expressions

Hello
I am using the following regex command to only allow numbers be inputted into a text field

Regex rgx = new Regex("[^0-9]"); 

however this obviously doesnt allow the inputting of positive and negative signs +/-. I would like to be able to input -9 for a negative value but when i do it comes back as 09 instead. Any help in getting the regular expression right would be appreciated.

Thanking You

1 Answer

1

this works for anyone who comes across a similar prob

Regex rgx = new Regex("[^-][^0-9]+");

The RegEx you posted does the opposite. Yours evaluates to false for all numbers. What you want is: Regex rgx = new Regex("[-]?[0-9]+");

–