PDA

View Full Version : Regular expression to except single dot ..?

murali1983
02-06-2007, 11:02 AM
hii

<input type="text" id="txt" maxlength="9" onkeypress="return numbersonly(event);">

onkeypress i am calling a function numbersonly(), in this function i need a regular expression which excepts a single dot not more than one dot.
once the user enters one dot, user can enter maximum of 2 numbers after dot
ex:
.11
and dot should not be allowed if user enters more than 6 numbers
ex: 1111111. this should not be allowed.
ex:111111.11 this is allowed

some examples
.11
11.11
111.11
1111.11
11111.11
111111.11

can anyone give a RE for this...

thnx
Murali.

nikkiH
02-06-2007, 05:36 PM
[0-9]+(\.[0-9][0-9])?

Arty Effem
02-06-2007, 06:21 PM
[0-9]+(\.[0-9][0-9])?That insists on two digits after any decimal, whereas I understand the requirement for a maximum of two. Also it needs start and end delimiters (^ \$) or it will pass anything containing a digit.

If one digit is allowed after the decimal:
/^\d+(\.\d{1,2})?\$/should get the assignment completed.

Philip M
02-06-2007, 06:39 PM
If one digit is allowed after the decimal:
/^\d+(\.\d{1,2})?\$/should get the assignment completed.

No - "and dot should not be allowed if user enters more than 6 numbers"

So:-

/^\d{0,6}(\.\d{1,2})?\$/

chump2877
02-06-2007, 06:56 PM
No - "and dot should not be allowed if user enters more than 6 numbers"

So:-

/^\d{0,6}(\.\d{1,2})?\$/

your regex doesn;t appear to allow integers that are greater than 6 digits long, i.e.: 88888888 (which appears to be perfectly valid for his uses).

you need some kind of conditional in there that says effectively: "allow a decimal point and up to 2 decimal places in the number only if the number is less than or equal to 999999"

felgall
02-06-2007, 07:01 PM
/^(\d{0,6}\.\d{1,2})|(\d+)\$/

Philip M
02-06-2007, 07:06 PM
your regex doesn;t appear to allow integers that are greater than 6 digits long, i.e.: 88888888 (which appears to be perfectly valid for his uses).

you need some kind of conditional in there that says effectively: "allow a decimal point and 2 decimal places in the number only if the number is less than or equal to 999999"

Yep, you are right.
So two tests are needed:-

/^d{7,}\$/ // OK if 7 or more digits and nothing but
/^\d{0,6}(\.\d{1,2})?\$/ // OK if up to six digits followed by one or two decimal digits.

Felgall has got it right in one expression.

Arty Effem
02-06-2007, 08:07 PM
Yep, you are right.
So two tests are needed:-

/^d{7,}\$/ // OK if 7 or more digits and nothing but
/^\d{0,6}(\.\d{1,2})?\$/ // OK if up to six digits followed by one or two decimal digits.

Felgall has got it right in one expression.I'm afraid it doesn't meet the spec under my tests.

I suggest: /(^\d{0,6}(\.\d{1,2})?\$)|^\d{7,}\$/

felgall
02-06-2007, 11:01 PM
I'm afraid it doesn't meet the spec under my tests.

I suggest: /(^\d{0,6}(\.\d{1,2})?\$)|^\d{7,}\$/

That is just a slightly longer variant of mine since the first part defines the decimal as optional for numbers up to 999999 and those numbers would be caught by the second test in my version so that making it optional in the first test is then unnecessary.

Arty Effem
02-07-2007, 12:17 AM
That is just a slightly longer variant of mine since the first part defines the decimal as optional for numbers up to 999999 and those numbers would be caught by the second test in my version so that making it optional in the first test is then unnecessary.

In your expression /^(\d{0,6}\.\d{1,2})|(\d+)\$/ , ^ and \$ have precedence over | , so effectively it evaluates to "anything ending with a digit".

It works if you write it: /^((\d{0,6}\.\d{1,2})|(\d+))\$/

felgall
02-07-2007, 01:36 AM
In your expression /^(\d{0,6}\.\d{1,2})|(\d+)\$/ , ^ and \$ have precedence over | , so effectively it evaluates to "anything ending with a digit".

It works if you write it: /^((\d{0,6}\.\d{1,2})|(\d+))\$/

I was sure those extra parentheses were there when I typed it in to start with. Maybe I just imagined they were there. Certainly they should be.