Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

i have a regex question!!!?

for java input validation

i need to only accept numbers and strictly one decimal point (dot).

if a decimal is entered, i only want to allow 2 decimal places following

any number of numbers before the decimal is fine

accepted:

123

123.45

.12

denied:

123.4

.123

123.

.

.1

thanks!

Update:

drspa44, since i have you here!

how about only numbers and up to 2 spots after the decimal and 2 before the decimal

decimal optional but if it is entered the above applies

accepted:

1

12

12.3

12.34

.1

.12

denied:

123

12.345

.

.123

2 Answers

Relevance
  • 7 years ago

    First question:

    \d*(\.\d\d)?

    Second question:

    \d{0,2}(\.\d\d?)?

    or this (it does the same thing):

    \d{0,2}(\.\d{1,2})?

    Warning: All of these regexps will accept an empty string! And there's no easy way to fix that. So don't forget to check if the string is not empty before you try to match the regex.

  • 7 years ago

    \d*(\.\d\d)?

Still have questions? Get your answers by asking now.