Hi folks
I Am trying to validate a single-line text box to include $ values like $1020.00 and this is the behavior on the custom field. I see no error and still can't have this behavior working
def field = getFieldById(getFieldChanged())
def val = field.getValue() as String
if (!val.matches("\$[0-9].[0-9]{2})")) {
field.setError("Amount must be in dollar format.")
} else {
field.clearError()
}
Your regex is both incomplete and with bad syntax
\$[0-9].[0-9]{2})
^ ^
| |
This means any |
character |
|
Where tis the corresponding open parens?
Also, it's best to use slashy strings for regular expressions in groovy to avoid the need for certain double escaping
Try like this:
if (!val.matches(/\$[0-9]*\.[0-9]{2}/)) {
...
}
I added * to mean any number of digits between "$" and "."
I also added \ in front of "." to mean a literal dot and not the regular expression that means "anything"
And I removed the extra parens
I recommend https://regex101.com/ to test and validate your regex.
Then double-test in the scriptrunner console with something like this with different "val" input until you are satisfied the logic works.
def val = '100.00'
if (!val.matches(/\$[0-9]*\.[0-9]{2}/)) {
"Amount must be in dollar format."
} else {
"good"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.