Behavior to limit character length and value in a custom text field

Scott Federman November 10, 2020

Hey all, 

I'm sure this is pretty easy but I'm pretty clueless with scripting. Could someone help me with a behavior that would only allow 2 characters and values of 0-25? Custom Field ID is 11850.

4 answers

3 accepted

1 vote
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2020

Hi @Scott Federman ,

which type does your custom field have? Is it a Number Field or Text Field (single line)? Thank you.

Scott Federman November 10, 2020

Hi @Hana Kučerová  its a text field...although i can change it to a number field if need be. 

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2020

Hi @Scott Federman

the field's type is ok :-).

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getValue() as String
if (!value.matches("[0-9]|1[0-9]|2[0-5]")) {
formField.setError("Your error message")
} else {
formField.clearError()
}

 Please add it as a server-side script to your customfield and try :-).

Scott Federman November 11, 2020

 @Hana Kučerová Its close! Its not allowing me to put any number in there at all though.

Capture.JPG

0 votes
Answer accepted
Scott Federman November 12, 2020

@Hana Kučerová i did notice one other problem. It works great on the edit screen, but there is no restriction from the view screen which is allowing me to put any number in the fields via inline editing.

Capture.JPG

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 13, 2020

@Scott Federman Please check the mapping (issue types, projects) and configuration of your Behaviour. Field's inline edit should not be not possible, when there's a validator, and Jira should automatically open the full edit screen, when you click on the pencil icon.

Scott Federman November 16, 2020

Interesting. You are correct. I'm not sure what was going on earlier. 

0 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2020

Hi @Scott Federman

did you try to move to another field, after you've entered the correct value (like 14)?

Scott Federman November 11, 2020

@Hana Kučerová ive tried moving to the next field as well as editing and have had no luck

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2020

Thanks!

Please try to test this code:

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getValue() as String
if (value) {
if (value != "test") {
formField.setError("error")
} else {
formField.clearError()
}
} else {
formField.clearError()
}

Then it should work like this:

  • Field is empty -> ok
  • Field is filled with some random text -> error
  • Field is filled with word test -> ok

Each time you change the value of the field, move to the next field. Then the validator should be called (you can check in browser's console - there should be activity on the Network tab)

Maybe there is something wrong with the regex, but it works for me :-(...

Which version of Jira and ScriptRunner do you have?

Scott Federman November 11, 2020

@Hana Kučerová here are my results

  • If empty then i can update the ticket
  • "Error" and error pops saying "Error" is not a valid number
  • "Ok an error pops saying  "Ok" is not a valid number.
  • Change to a number and it still says "Ok" is not a valid number.

We are using Jira 8.4.1 and scriptrunner 5.6.6.1-jira8

Capture1.JPG

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2020

@Scott Federman It seems to me, that your custom field must have Number Field type (not Text Field (single line)). Is it possible?

Are there any other behaviours defined for your project?

Thanks.

Scott Federman November 11, 2020

@Hana Kučerová You know i could have sworn they were text fields but you are correct. They are number fields.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2020

@Scott Federman Thing happens :-)...

If it is a number, what about the values like 14.0 ? Are they valid or not? And what about 14.5? Thanks...

Scott Federman November 12, 2020

Thanks @Hana Kučerová

No need for decimals. Everything will be a whole number.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 12, 2020

@Scott Federman Last try :-)

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getFormValue() as String
if (value) {
if (!value.matches("[0-9]|1[0-9]|2[0-5]")) {
formField.setError("Your error message")
} else {
formField.clearError()
}
} else {
formField.clearError()
}
Scott Federman November 12, 2020

We have a winner!!!!

 

Thank you for everything @Hana Kučerová You are a magician. 

Tomáš Doležel October 18, 2021

Hello @Hana Kučerová
I am also rookie in scripting. I spent hours of time to change if condition for custom field but didn´t find working solution. My condition should be: 

Value of the custom field must contain exact 7 digits (not less, not more, only digits not letters).

Could you help me with this condition please? Thank you very much. 
 

Hemanth Kumar September 22, 2022

@[deleted]  first of all, thanks for the attempts and patience. Can you please help me understanding below line ?

if (!value.matches("[0-9]|1[0-9]|2[0-5]")) {

 I need to set the limit of an field to accept value only if its between (60 - 8400), this is numeric field these are values in seconds

Hemanth Kumar September 26, 2022

@Hana Kučerová I was able to create script with some help of google/community and figured out to ranging from 60 to 100, but can you assist how to fix the range to 3 digit and 4 digit value. (as mentioned in above - 60 - 8400 is my requirement)

 

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getFormValue() as String
if (value) {
    if (!value.matches("[6-9][0-9]|100")) {
        formField.setError("value should be bet. 60-8400")
    } else {
        formField.clearError()
    }
} else {
    formField.clearError()
}
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2022

@Hemanth Kumar 

Please try:

(6[0-9]|[7-9][0-9]|[1-9][0-9]{2}|[1-7][0-9]{3}|8[0-3][0-9]{2}|8400)

Hemanth Kumar September 26, 2022

@Hana Kučerová  It worked thanks a lot. But i would like to understand the format styling please.

I tried (("[6-9][0-9]|100") && ("[0-9][0-9][0-9]|1000")) assuming - 60 to 100 and 101 to 1000, but it didn't worked.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2022

@Hemanth Kumar 

6[0-9] .. 60 - 69

[7-9][0-9] .. 70 - 99

[1-9][0-9]{2} .. 100 - 999

[1-7][0-9]{3} .. 1000 - 7999

8[0-3][0-9]{2} .. 8000 - 8399 

8400 .. 8400

Like Hemanth Kumar likes this
Hemanth Kumar September 26, 2022

@Hana Kučerová  thanks a lot, it helped

Kishore D January 19, 2023

Hi @Hana Kučerová ,

Thanks for your script. it worked for me too.

Along with this script, i have extended requirement ie; my text field should not exceed more than 10 characters and it should allow only 0-9 numbers. 

Help required from you:  

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getValue() as String
if (!value.matches("[0-9]{1,256}")) {
formField.setError("Your error message")
} else {
formField.clearError()
}

 can you tell us how can we add "|" as multiple value separator. 
[0-9]{1,256} is to keep field max length  is 256 characters. we need to modify script as to add "|" as multiple value separator.

Expected:
Field_name= 1234|456|7890|..

can you help?

Venkat_Suresh February 16, 2024

@Hana Kučerová My requirement is in the Numeric field type (Data Center) value has to be between 00000 and 99999. Using https://3widgets.com/, I got the expression as 

(0{4}[0-9]|0{3}[1-9][0-9]|00[1-9][0-9]{2}|0[1-9][0-9]{3}|[1-9][0-9]{4})


Wrote the below Behavior on the field as below and it does not work (accepts decimals, more than 5 characters etc)

 

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getFormValue() as String
if (value) {
    if (!value.matches("(0{4}[0-9]|0{3}[1-9][0-9]|00[1-9][0-9]{2}|0[1-9][0-9]{3}|[1-9][0-9]{4})") {
        formField.setError("value should be bet. 00000-99999")
    } else {
        formField.clearError()
    }
} else {
    formField.clearError()
}
Any suggestions on where I am going wrong?
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 18, 2024

@Venkat_Suresh Maybe 

[0-9]{5}

?  But you probably need to change it to the text field because of the leading zeros.

Venkat_Suresh February 19, 2024

@Hana Kučerová Even after changing to Text field type, below behavior does not error when entered characters. 


Character field validations

import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getFormValue() as String
if (value) {


if (!value.matches("(0{4}[0-9]|0{3}[1-9][0-9]|00[1-9][0-9]{2}|0[1-9][0-9]{3}|[1-9][0-9]{4}|[0-9]{5})") {
formField.setError("value should be bet. 00000-99999")
} else {
formField.clearError()
}
} else {
formField.clearError()
}

Any other ideas? or pointers?

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2024

@Venkat_Suresh Have you tried 

[0-9]{5}

instead of

(0{4}[0-9]|0{3}[1-9][0-9]|00[1-9][0-9]{2}|0[1-9][0-9]{3}|[1-9][0-9]{4}|[0-9]{5})

?

Venkat_Suresh February 19, 2024

@Hana Kučerová Thank you for the response so far. 

Tried as below on a Character field


import com.onresolve.jira.groovy.user.FormField

FormField formField = getFieldById(getFieldChanged())
String value = formField.getFormValue() as String
if (value) {
    if (!value.matches("[0-9]{5}") {
        formField.setError("value should be bet. 00000-99999")
    } else {
        formField.clearError()
    }
} else {
    formField.clearError()
}

No luck. Something is wrong.
But on a Number type of field tried as below and is working. But since 00013 is an allowed value and having a number field will reduce value to 13 instead of retaining as 00013, I have to have it as a Character field
----------------
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def XSMClarityID = getFieldById(getFieldChanged())
def FieldValue = XSMClarityID.getValue() as Long;
if (FieldValue.toString().length() < 5)
{
    XSMClarityID.setError("ID Value must be 5 digits")
}else

if(FieldValue < 0 || FieldValue > 99999 )
{
    XSMClarityID.setError("ID Value must be between 00000-99999")
}else
{
    XSMClarityID.clearError();
}
--------------- 
Venkat_Suresh February 27, 2024

Finally figured the solution on a text field

 

import java.lang.String

def field = getFieldByName("Custom Field name")
if ((field?.value as String)?.length() > 5 || (field?.value as String)?.length() < 5 || (field?.value as String)?.contains(".") || !(field?.value as String)?.isNumber()) {
field.setError("Value should be exactly 5 numbers between 00000 to 99999. No Decimals or alphabets allowed")
} else {
field.clearError()
}
0 votes
Peter Preston
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 16, 2020

@Scott Federman - glad you were able to resolve the validation problem. If this is something you need to build often, you could try ProForma (or ProForma Lite - a free version of the app) for a simpler way to build validation. Full disclosure, I'm on the ProForma team.

 

Screen Recording 2020-11-17 at 09.11.34.gif

Suggest an answer

Log in or Sign up to answer