register
other register

Wednesday, September 16, 2009

Java Regular Expression Experience.

If the input only allows the number format like the following: 1, 11, 1.1, 1.11, 11.1, 11.11.

class Test {
java.math.BigDecimal price

static constraints = {
price(blank: false, validator: {val, obj ->
// this will only allow those 33.33 format
if (!Pattern.matches("\\d{0,2}\\.\\d{0,2}", val.toString())
&& !Pattern.matches("\\d{0,2}", val.toString())) {
return ['digital']
}
}
)
}

Please refer to Java Regular Expression docs for more info.


In addition, make sure the mysql table column price with the right (enough) type: decimal (10, 2).

No comments: