Simple Script Validators - exact number of digits


I want to have a validation for an optional field that has exactly 8 digits.

My code is:

 // Import Regex

import java.util.regex.*;

// Construct The Pattern to Match on

Pattern pattern = Pattern.compile("([0-9]){8}");

// Check if the PhoneNumber Field is a match

cfValues['PhoneNumber'] == null || pattern.matcher(issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("PhoneNumber")).toString())  


The problem is that allows can insert more than 8 digits.

2 answers

1 accepted

0 votes
Tarun Sapra Community Champion Aug 09, 2017

Try this


Anna you were missing the dollar sign ($) in your snippet which means "don't add anything after me" as dollar sign means end of line.

It works with Pattern.compile("^([0-9]{8})\$")

Thank you

I want to ask another case: If I need the number of digits to be between 5 to 8 digits, how I will write the command?

Tarun Sapra Community Champion Aug 10, 2017

Just make it

Tarun Sapra Community Champion Aug 10, 2017

If it works for you, please accept the answer. thanks.

What if we want to add special characters like "-" to the above snippet? e.g 9956-56

0 votes
Tarun Sapra Community Champion Dec 05, 2017

Hi @Stefani Kaimakliotou

Your requirement of "9956-56" can be implemented with the snippet


i.e. digits with "-" (dash) and total length of 5 to 8 

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,086 views 13 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot