Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Regular expression to except single dot ..?

1. ## Regular expression to except single dot ..?

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.

• [0-9]+(\.[0-9][0-9])?

• Originally Posted by nikkiH
[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:
Code:
`/^\d+(\.\d{1,2})?\$/`
should get the assignment completed.

• Originally Posted by Arty Effem
If one digit is allowed after the decimal:
Code:
`/^\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})?\$/

• Originally Posted by Philip M
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"

• /^(\d{0,6}\.\d{1,2})|(\d+)\$/

• Originally Posted by chump2877
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.

• Originally Posted by Philip M
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,}\$/

• Originally Posted by Arty Effem
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.

• Originally Posted by felgall
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+))\$/

• Originally Posted by Arty Effem
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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•