How to construct Simple Script Validators?

Jeff Peters February 16, 2016

Thanos,

Good day.

I need some help in constructing several simple scripted validators for the following scenarios:

  • Summary field ((Field Type = Text Field (single line))) only contains a 10 numeric character string. For example, 1234567890 is valid.
  • Custom field Policy Number (Field Type = Text Field (single line)): Only a 9 character string: 6 numeric, "-", and 2 numeric. For example, 123456-12 is valid.
  • Custom field Provider (Field Type = Text Field (single line)): Only a 10 character string: 1 alpha character followed by 9 numeric characters. For example, A123456789 is valid.
  • Custom field Overpaid Amount (Field Type = Text Field (single line)): Only a numeric string with hundredths value (cents) between 0.01 and 99999.99. For example, 12345.12 is valid.

Many thanks for your assistance!

2 answers

2 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 23, 2016

Hi Jeff,

I have included some examples in the code block below which you can use to create some simple validators for your scenarios above.

// Summary Validator Code

// Import Regex
import java.util.regex.*;
// Construct The Pattern to Match on
Pattern pattern = Pattern.compile("([0-9]){10}");
// Check if the Summary Field is a match
Matcher matcher = pattern.matcher(issue.summary);

// Policy Number Validator Code

import java.util.regex.*;
// Construct The Pattern to Match on
Pattern pattern = Pattern.compile("([0-9]){6}[-]([0-9]){2}");
// Check if the Summary Field is a match
Matcher matcher = pattern.matcher(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Policy Number")).toString());

// Provider Validator Code

import java.util.regex.*;
// Construct The Pattern to Match on
Pattern pattern = Pattern.compile("([A-Z]){1}([0-9]){9}");
// Check if the Summary Field is a match
Matcher matcher = pattern.matcher(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Provider")).toString());

// Overpaid Amount code

import java.util.regex.*;
// Construct The Pattern to Match on
Pattern pattern = Pattern.compile("([0-9]+)[.]([0-9]+)");
// Check if the Summary Field is a match
Matcher matcher = pattern.matcher(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Overpaid Amount")).toString());

Also in the screenshots below I have shown how I have configured the first 2 validators. You may wish to tweak the regular expressions defined inside the pattern variable in order to further suit your requirements.

 Summary.pngPolicyNumber.png

I hope this helps

Many Thanks

Kristian

Jeff Peters February 23, 2016

Kristian,

Many thanks for the concreteness of your reply to me, a neophyte developer/coder.

I greatly appreciate your time and assistance.

0 votes
Thanos Batagiannis _Adaptavist_
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 16, 2016

Hi Jeff,

You can use regular expressions for you checks and because the conditions you mention are not anything 'extreme', there are a lot of online tools (for example http://regexr.com/) that can help you. Now once you have the regular expression you can easily make a comparison

Now a few notes regarding on how to retrieve the info you need from the issue. issue.summary for the summary and cfValues['Policy Number'] for custom fields. For more information have a look at validators documentation, if you still need more assistance give me a shout (and add '@' in front in order to mention me).

Kind regards

 

JamieA
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 22, 2016
Jeff Peters February 23, 2016

Thanos,

Many thanks for the http://regexr.com/ tip.

Best regards!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events