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 !!!!
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)