Tuesday, December 1, 2009

Regular Expression for Numbers only validation


For the new site i'm working on, I wanted to validate the user input for allowing only to enter a numeric value with or without decimal points. So i decided to use a Regular Expression. After some time I came up with his solution.

Ok If you want to match a floating point numbers use this ^[0-9]*\.?[0-9]+$
You can use this to validate whether a user has entered a number (2) or number with decimals (2.45453).

But if you only want to match whole numbers like 2, 3, 333, 545 etc. use this ^[0-9]+$

How to match a specific number of numeric characters
For example, If I want 10 digit numeric character set - ^[0-9]{10}$

0 comments:

Post a Comment