Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,363,993
Community Members
 
Community Events
168
Community Groups

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

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 Nov 10, 2020

Hi @Scott Federman ,

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

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

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 :-).

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

Capture.JPG

0 votes
Answer accepted

@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 Nov 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.

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

0 votes
Answer accepted
Hana Kučerová Community Leader Nov 11, 2020

Hi @Scott Federman

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

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

Hana Kučerová Community Leader Nov 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?

@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 Nov 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.

@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 Nov 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...

Thanks @Hana Kučerová

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

Hana Kučerová Community Leader Nov 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()
}

We have a winner!!!!

 

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

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. 
 

@Hana Kučerová  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

@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 Sep 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)

@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 Sep 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

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

0 votes

@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
TAGS

Atlassian Community Events