Using Workflow 'Regular Expression Check' Validator to Restrict Text Field in Number Format

Still need help?

The Atlassian Community is here for you.

Ask the community

Problem

Commas are automatically added as Thousands Separators when displaying a Custom Field with a Type of Number Field. This does not make sense if we use the Number Field for Phone Numbers, Postal Codes, etc. Suggestion tickets have been raised to improve this functionality at JRASERVER-7582 & JRACLOUD-7582.

Prerequisites

  • Regular Expression Validators are a built-in feature in Jira Cloud.
  • For self-hosted JIRA, this add-on provides the Validators used here: JSU - Suite Utilities for Jira.

Resolution

  1. Create the Custom Field (eg: Phone Number) in *Text Field (Single Line)* and add them to the corresponding Screens.
  2. Navigate to the Workflow and add a Regular Expression Validator to make sure only numbers can be entered for these Fields.
    1. Go to Project Setting > Workflows
    2. Edit the Workflow for the affected Issue Type
    3. Switch to Diagram mode
    4. On Create transition, click on the Validators
    5. Choose Add Validator > Regular Expression Check
      1. Field to validate: <Field created in Step 1>
      2. Regular Expression:

        ^[0-9]*$

        Regular Expression for Other Scenarios

        If you would like to restrict the field to a specific length: 

        ^[0-9]{n}$


        If you would like to allow it to empty:

        ^[0-9]{0-n}$


        If the field is required:

        ^[0-9]{m-n}$

        n refers to the maximum number of the length

        m refers to the minimum number of the length

Note: This enforces value formatting on Issue Creation. This does not prevent a User from editing the Field after the Issue is Created and inputting a different format for the number. A Validator can be placed on each Transition for the Issue Type, but so long as the Field can be Edited outside of Transitions, it can be set to a value that does not match the Regular Expression.

Regex Solution for Phone Numbers

If you are looking to use regex for a 10-digit phone number you can use the following regex with the following scenarios:

  • 123-456-7890
  • (123) 456-7890
  • 123 456 7890
  • 123.456.7890
  • +91 (123) 456-7890

Regex options:

For all number formats mentioned above you can use:

^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

If you don't want to use non-US numbers in the Validator you can use:

^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

And lastly, if you want to use unformatted numbers you can use:

^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Alternative Solution

For customers who don't have the JSU - Suite Utilities for Jira installed, the plugin Unique Regex Custom Field provides a free alternative which allows you to create custom fields with a build-in regular expression check.


Last modified on Jan 20, 2023

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.