Forums

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

Need to use regex validation on Scriptrunner for a field validation

Luiz Gregorio September 2, 2019

Hello all.

I have a text field called "initial value" and I need to implement a scriptrunner validator on this field while creating an issue on Jira Service Desk.

The value within this field must only be a number between 0 and 10,000 (0 <= N <= 10000)

I have tried the following script but, although it throw no errors, the validation is nor working properly.

 

! cfValues['Initial Value'] || cfValues['Initial Value'] ==~ /\d([0-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-8][0-9]{3}|9[0-8][0-9]{2}|99[0-8][0-9]|999[0-9]|10000)/

Can anybody throw some light into this?

Thank you!! 

2 answers

1 accepted

3 votes
Answer accepted
Rafael Pinto Sperafico
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.
September 2, 2019

Hi @Luiz Gregorio ,

Is there any particular reason for using a regex and not an if statement? In any case, you could use:

# true from 0 to 10k

/^(10000|([0-9]{0,4}))$/

Kind regards,
Rafael

Luiz Gregorio September 2, 2019

Hi Rafael, not any particular reason, in fact I am not an expert on scripts, I found this solution online.

Luiz Gregorio September 4, 2019

I have tested using your regex condition and it worked like a charm.

If anyone needs this, the correct line would be:

 

! cfValues['Initial Value'] || cfValues['Initial Value'] ==~ /^(10000|([0-9]{0,4}))$/

Thank you for your help

Dinesh
Contributor
January 26, 2021

I have a text field and would like to allow only number in the format of "#.#.#" and each # accepts a max of 3 digits".

I have configured something like this.

val.matches("([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).

 

It works fine with numbers but it is allowing users to enter all the special characters. Can you please help me with regex condition excluding the special characters. 

Rafael Pinto Sperafico
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 9, 2021

Hi @Dinesh ,

(dot) . Matches any character except linebreaks.

Please, try the following:

import java.util.regex.Pattern;
import org.apache.log4j.Level;
log.setLevel(Level.DEBUG);

private boolean isValid(String value) {
final String REGEX = /([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/;

final Pattern PATTERN = Pattern.compile(REGEX);

return PATTERN.matcher(value).matches();
}

log.debug(isValid("0.0.0"));
log.debug(isValid("0.0.0.0"));
log.debug(isValid("0.0.0a"));
log.debug(isValid("0^0.0"));
log.debug(isValid("0.0.0"));
0 votes
Bastian Stehmann
Community Champion
September 2, 2019

Hi @Luiz Gregorio ,

I think there are two kinds of script runner validates. Are you using the simple one? Otherwise you must throw an exception if the validator should prevent someone of creating an issue (InvalidInputException, I think ,but can not check this currently)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events