Reg exp not working...

K

Kim

Hello,

I want valid input to be 1 to 9 digits followed by an optional decimal
point, followed by an optional 1 to 4 digits. I think this should
work:

re = /\d{1,9}\.?\d{1,4}/

The problem I am having is that it is not restricting the number of
digits to the right and left of the decimal point. So my "range" parts
({1,9} and {1,4}) are probably wrong in some way but I cannot figure
out how... I have researched the syntax and this seems to make sense to
me. Please help!

Thanks,

Kim
 
M

Michael Winter

On 21/10/2005 16:26, Kim wrote:

[snip]
re = /\d{1,9}\.?\d{1,4}/

The problem I am having is that it is not restricting the number of
digits to the right and left of the decimal point.

Only the decimal point is optional, not the decimal part (which is the
intent, I assume). It also only needs to match a substring. Additional
characters, including non-numeric ones, can appear before and after the
matched pattern.

/^\d{1,9}(\.\d{1,4})?$/

The ^ and $ assertions ensure that the pattern matches characters at the
start and end of the string, respectively. In other words, the entire
string must match the pattern.

[snip]

Hope that helps,
Mike
 
K

Kim

I realized I was missing the second ? after I posted. Thanks so
much!!! That works perfectly! :)
 
E

Evertjan.

Kim wrote on 21 okt 2005 in comp.lang.javascript:
I want valid input to be 1 to 9 digits followed by an optional decimal
point, followed by an optional 1 to 4 digits. I think this should
work:

re = /\d{1,9}\.?\d{1,4}/


result = /\d{1,9}\.?\d{1,4}/.test(myString)

this would accept 1-9 digits + a optional . + 1 to 4 digits

anyware in a long string.

"qwerty1234567891234qwerty" would be accepted.
The problem I am having is that it is not restricting the number of
digits to the right and left of the decimal point. So my "range" parts
({1,9} and {1,4}) are probably wrong in some way but I cannot figure
out how... I have researched the syntax and this seems to make sense to
me. Please help!

Try:

function result(x){
return /^\d{1,9}(\.\d{1,4})?$/.test(x)
}

alert(result('12')) //true
alert(result('qwerty12')) //false
alert(result('12.23')) //true
alert(result('12.')) //false
alert(result('.23')) //false
alert(result('0.23')) //true
alert(result('000000000.2300')) //true ????

This what you want?

================

or without accepting starting (except in 0.23) and final zeros:

function result(x){
return /^(([1-9]\d{0,8})|0)(\.\d{0,3}[1-9])?$/.test(x)
}

alert(result('12')) //true
alert(result('qwerty12')) //false
alert(result('12.23')) //true
alert(result('12.')) //false
alert(result('.23')) //false
alert(result('0.23')) //true
alert(result('000000000.2300')) //false !!!!
 
K

Kim

I am using: /^\d{1,9}(\.\d{1,4})?$/

Thank you for you quick and effective responses!

:)

Kim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,588
Members
45,093
Latest member
Vinaykumarnevatia00

Latest Threads

Top