简体   繁体   中英

Amount validation - regular expression in Java

In java class I need regular expression for amount validation with the following conditions:

  1. Maximum price is 9999.99
  2. Minimum amount is 1
  3. Decimal values is optional (Paise is optional) that is 9999 is Valid

I write the following regular expression but it always return false for both valid and invalid inputs.

Here's example:

private static final String PRICE_PATTERN = "((/d{1,4})(((//.)(/d{0,2})){0,1}))";

public PriceCheck() {
    pattern = Pattern.compile(PRICE_PATTERN);
}

public boolean validate(final String username) {
    matcher = pattern.matcher(username);
    return matcher.matches();  // always return false;
}

请改用以下表达式:

private static final String PRICE_PATTERN = "((\\d{1,4})(((\\.)(\\d{0,2})){0,1}))";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM