问题
I'm trying to validate a textfield to it's only numbers and one decimal, but the decimal keeps causing the validator to go off:
TextField tf = new TextField();
tf.addValidator(new RegexpValidator("^\\d+$", facilityId.getCaption() + "Only numbers and one decimal allowed."));
So I got the numbers to work, but how do I tell the validator that the "." is okay?
Note: Add-Ons are not an option.
回答1:
This regex should work
/^\d+\.\d$|^\d+$/
Breakdown
^ - start of line
\d+ - One or more digit
\. - Escaping a period otherwise it will match any character
\d$ - It ends with one digit after the decimal
| - Or
^\d+$ - It has any number of digits without a decimal
来源:https://stackoverflow.com/questions/34054129/vaadin-how-to-make-a-validator-accept-numbers-and-one-decimal-only