Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaFX 2.0 - Regex for Float in Text Field

935838Jun 22 2012 — edited Jun 22 2012
Quick question, I am trying to add a regular expression that will only allow input of a float value.


Using
private final static String REG_EXP = "[-]?[0-9]+(\\.[0-9]+)?";

@Override public void replaceText(int start, int end, String text) {
if (text.matches(REG_EXP) || text.hashCode() == 0) {
super.replaceText(start, end, text);
}
}

When ever i enter say first char "-" the text.matches returns false. Even though i have this as optional within the regex? It will also not allow me to enter the decimal (.) Although ive trailed through the various java float examples for regex and this expression appears to be reoccur.

Could anyone advise what im doing wrong here?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 20 2012
Added on Jun 22 2012
3 comments
2,079 views