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

How To Conditionally Enforce REGEX Validation Depending on A Custom Field Value

Ricky Wang Lin
Contributor
August 10, 2021

Hi there.  Using either ScriptRunner / JSU / JMWE, is there a way to satisfy the following conditions:

Fields:

  • *File Source - Single Select field.
    • Google
    • Yahoo
    • Bing
  • *File Source URL - Single text field

Conditions:

  • File Source URL will will need to begin to match the beginning depending what File Source value is selected (e.g https://google.com)
  • File Source URL will need to end to match the ending in pdf

Examples:

We've been able to make some progress to check use ScriptRunner's Regular expression validator [ScriptRunner] to be able to make something like this happen with the following:

^https:\/\/google.com\/.*.pdf

 However, I struggle to expand the script above to interact with another field.  Any insights is appreciated!

1 answer

0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 10, 2021

Hi @Ricky Wang Lin 

with JMWE, you could use a Scripted (Groovy) Validator with code like this:

switch (issue.get("File Source")) {
case "Google": return issue.get("File Source URL") ==~ /^https:\/\/google.com\/.*.pdf/
case "Yahoo": return issue.get("File Source URL") ==~ /^https:\/\/yahoo.com\/.*.pdf/
default: return true //no check if another source
}
Ricky Wang Lin
Contributor
August 12, 2021

Hi David.  Thank you the response!  Is it also possible to conditionally display a different error message for each one (depending on the File Source value)?

Example:

  • If someone enters "www.google.com" instead of "https:google.com",  display error message 1
  • If someone enters "www.yahoo.com" instead of "https:yahoo.com",  display error message 2
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 12, 2021

Absolutely! Instead of returning a boolean value (true/false), you can return a String that will be the error message:

switch (issue.get("File Source")) {
case "Google": return issue.get("File Source URL") ==~ /^https:\/\/google.com\/.*.pdf/ ?: "The URL is not from Google"
case "Yahoo": return issue.get("File Source URL") ==~ /^https:\/\/yahoo.com\/.*.pdf/ ?: "The URL is not from Yahoo"
default: return true //no check if another source
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events