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
Accepted answer
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 Sign up to answer
Community showcase
Published Wednesday in Jira

Make your Atlassian Cloud products more secure: our NEW admin security guide

Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...

249 views 0 6
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