Vaadin: How to make a validator accept numbers and one decimal only

怎甘沉沦 提交于 2019-12-23 05:35:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!