views:

138

answers:

1

hi,

I am trying to use a regular expression validation to check for only decimal values or numeric values. But user enters numeric value, it don't be first digit "0"

How do I do that?

+2  A: 

A digit in the range 1-9 followed by zero or more other digits:

^[1-9]\d*$

To allow numbers with an optional decimal point followed by digits. A digit in the range 1-9 followed by zero or more other digits then optionally followed by a decimal point followed by at least 1 digit:

^[1-9]\d*(\.\d+)?$

Notes:

  • The ^ and $ anchor to the start and end basically saying that the whole string must match the pattern

  • ()? matches 0 or 1 of the whole thing between the brackets

Update to handle commas:

In regular expressions . has a special meaning - match any single character. To match literally a . in a string you need to escape the . using \. This is the meaning of the \. in the regexp above. So if you want to use comma instead the pattern is simply:

^[1-9]\d*(,\d+)?$

Further update to handle commas and full stops

If you want to allow a . between groups of digits and a , between the integral and the fractional parts then try:

^[1-9]\d{0,2}(\.\d{3})*(,\d+)?$

i.e. this is a digit in the range 1-9 followed by up to 2 other digits then zero or more groups of a full stop followed by 3 digits then optionally your comma and digits as before.

If you want to allow a . anywhere between the digits then try:

^[1-9][\.\d]*(,\d+)?$

i.e. a digit 1-9 followed by zero or more digits or full stops optionally followed by a comma and one or more digits.

mikej
+1 (but use "^[1-9][0-9]*$" for those older regex engines that don't support PCRE).
paxdiablo
Make that `^([1-9]\d*|0)$` if zero is a valid number.
RoToRa
thanks it's done. But i forgot say something. Users entered value also include "," character.. how do i do please
ilkdrl
sorry mike but not worked. I'm sorry my English skills but I'll try to explain my question.according to your validation expressionuser can not enter firs digit 0. okay it's done user can enter decimal number. okay it's done but user should enter 250,50 instead of 250.50Your validation code don't allow to enter "," character.
ilkdrl
No problem, I think the confusion was because the UK uses **.** to separate the the integral and the fractional parts of a decimal number and uses **,** to make long numbers more readable e.g. I would write a million as 1,000,000 I guess you are in a country that uses **,** as the decimal point. See this Wikipedia page for more details: http://en.wikipedia.org/wiki/Decimal_separator
mikej
thank you very much
ilkdrl
mike iam sorry man, sure your answer was done but again i forgot say something. It icludes "," and "."^[1-9]\d*(\.\d+)?$ for "."^[1-9]\d*(,\d+)?$ for ","i didnot integrate these. how can i do it?
ilkdrl
Another update above. If this is still not exactly right then you will need to update the question to include some examples of numbers that are valid and numbers that are not valid to make things clearer.
mikej
thank you very much mike, its that..
ilkdrl