What is the regular expression for valdating a 'price' in a jtextfield
Posted
by wniroshan
on Stack Overflow
See other posts from Stack Overflow
or by wniroshan
Published on 2010-04-29T17:59:36Z
Indexed on
2010/04/29
18:07 UTC
Read the original article
Hit count: 400
I want to validate a jtextfield as user inputs a price of some item.
I have a perfectly working code which beeps and discard if user inputs some character that doesnt match the regular expression.(for regular expression "[0-9]+" it accepts any number of digits and beeps if a letter is given as input)
But the problem is I want to validate an input like "12500.00". Format should be any number of digits before decimal point and two digits after the decimal point. eg: ddd.dd
I tried the following regular expression. But the i doesnt allow user to input "." character. It allows user to input any number of digits but when "." key is pressed input is not accepted.
here is the regular expression I used. "[0-9]+ | [0-9]+. | [0-9]+.[0-9]{1} | [0-9]+{2}"
important: user input is validated character by character as the user inputs them to the jtextfield.
© Stack Overflow or respective owner