Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to write a groovy validation simple script runner for valid email regular expression

Pradeep Kumar
Contributor
February 12, 2018

I want to validate that when user enters valid email address or not in a custom field.

 

we are seeing below error.Error 4.png

Please help.  Code below.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def myFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Client Submitter Email"))

/* Set debugging level */
log.setLevel(org.apache.log4j.Level.DEBUG)

def regexStr = /[a-zA-Z0-9.'_%+-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,4}/

log.debug("myFieldValue: " + myFieldValue)

if (myFieldValue == null || myFieldValue == "") {
return true
}

try {
if (myFieldValue.matches(regexStr)){

log.debug("Valid email address: " + myFieldValue)
return true

} else {
log.debug("Invalid email address" + myFieldValue)
return false
}
}
catch(err) {
log.debug("Please enter valid email address")
log.debug(err.getMessage())
return false
}

/* Re-set debugging level */
log.setLevel(org.apache.log4j.Level.WARN)

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2018

Assuming 'myFieldValue' is a string then I guess you've ran into a known issue with static type checking in ScriptRunner not always being accurate. More info here. With that in mind I would simply ignore this error and see if the actual validator works.

Edward Greathouse
Contributor
June 8, 2018

Did this script actually work?

Suggest an answer

Log in or Sign up to answer