I'm currently searching how to write correctly a regex for this application :
1 - A number without "." with a length of 1 to 5 digits
=> /^(\d{1,5})$/
2 - A number with "." with a length of 1 to 5 digits before the "." and 1 to 4 digits after the "." or a number starting with "." with a length of 1 to 4 digits after the "."
=> /^(\d{1,5})?\.?(\d{1,4})?$/
I tried to use a or operator "|", but it doesn't work ;(
=> /^(\d{1,5})?\.?(\d{1,4})?$|^(\d{1,5})$/
I do not understand why, it's my first java script regex and i'm not sure to use well the "|" operator.
Following the answers I would like to obtain with 1 regex :
123 => ok
12345 => ok
123456 => not ok
12345.2156 => ok
123456.12 => not ok
12345.12345 => not ok
Thank you very much for your help. Have a nice day.
Etienne
Both rules rolled in to one:
^\d{1,5}$|^\d{0,5}\.\d{1,4}$
Here is a working example
You could check the second part as optional.
function check(v) {
return /^(?=.)\d{0,5}(\.\d{1,4})?$/.test(v);
}
console.log(['', '.123', 123, 12345, 12345.2156, 123456, 123456.12, 12345.12345].map(check));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With