Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,552,543
Community Members
 
Community Events
184
Community Groups

Scriptrunner behavior regex

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()
}

 

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 29, 2021

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

Thank you for sharing your knowledge and insights. Overlooked () and regex is always tricky for me. Anyways Thanks again.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 29, 2021

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"
}
Like Sysad likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events