Enforce regex on a single line text custom field

Hi,

I have a single line text custom field on a project with 7000+ issues that is called Due Time

Users have been entering the time in any format into that custom field.

What I'd like to do is enforce the simple PHP-style regex below onto the field to standardize it.

\d\d?:\d\d (AM|PM)$

Is there an easy way to do this with ScriptRunner's Scripted Fields?

I think I would need to copy my existing custom field over to a new Scripted Field first, but I'm not quite sure where to go from there, or if there is a better solution.

 

Thanks,

Raina

2 answers

1 accepted

This widget could not be displayed.

Isn't a better solution just to use a date-time custom field? Failing that you can use a DateFormat in a simple scripted validator as @Vasiliy Zverev suggests. 

@Jamie Echlin [Adaptavist] I complete agree with you and @Vasiliy Zverev!

The problem that we were having was that our users were not properly selecting the time on the date-time custom field in the past.

Rather than re-invent the wheel here, I'm going to add the new date-time custom field to replace my old single line text Due Time field and my date picker for Due Date with a date-time custom field called Due Date and Time on the create screen along with a bold red note in the field configuration that says something along the lines of "please set your due time when selecting your date on the calendar".

Thank you both!

This widget could not be displayed.
Vasiliy Zverev Community Champion Jun 24, 2016

You can use SimpleDateFormat java class to parse string to Date. Here is a example:

import com.atlassian.jira.component.ComponentAccessor

import java.text.ParseException
import java.text.SimpleDateFormat

//here we create all requared date fortas
SimpleDateFormat format1 = new SimpleDateFormat("yyyy/MM/dd")
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd")

SimpleDateFormat resultFormat = new SimpleDateFormat("dd/mm/yyyy")

String stringToParse = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName").getValue(issue)
Date parsedDate = null;
//here we parse it
try {
    parsedDate = format1.parse(stringToParse).toString()
} catch (ParseException e){
    parsedDate = format2.parse(stringToParse).toString()
}

if(parsedDate != null)
    return resultFormat.format(parsedDate)
else
    return "not parsed"
Vasiliy Zverev Community Champion Jun 27, 2016

Agree with @Jamie Echlin [Adaptavist], use this code to parse date and use date or datetime filed further and you will never have such troubles anymore.

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked yesterday in Data Center

Hi! I'm Cameron Deatsch, enterprise enthusiast and Head of Server at Atlassian. AMA!

Hey team! I'm Cameron Deatsch, Head of Server at Atlassian. At our European Summit just a few weeks ago, I spoke about the latest regarding our Server and Data center products and o...

19,244 views 4 4
View question

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