How to construct Simple Script Validators?

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

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

Kristian,

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

I greatly appreciate your time and assistance.

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

 

Thanos,

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

Best regards!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

117 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you