Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need scriptruner code for single line text field with restricting 0-9 length from create, edit scren

Kishore D
Contributor
January 19, 2023

Need scriptrunner code for single line text field with restricting 0-9 numbers with 10characters length from create, edit screen.

I am able to keep restrict in create screen with validator in workflow for create transition.

regex used in validator is: ^[0-9]{1,10}$

But its allowing to update field from view/edit screen. 

Can i get help here?

1 answer

0 votes
Vamsi Kandala
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.
January 19, 2023

Hi @Kishore D

Are you using Behaviors?

If so,

Add the mappings to the related project and issue type.

Select the field for which you need this validation for.  And click on 'Add server-side script'.

Add the below code:

def field = getFieldByName("Name of the custom field")

//Or
//def field = getFieldById("customfield_xxxxx")

if ((field?.value as String)?.length() > 10) {
    field.setError("Length of the field should not be more than 10 characters")
}
else {
    field.setError("")
}
I am not using regex here but comparing with the length of the string.  It is working for me and we have implemented this in our project.
Hope this helps.
Thanks,
Vamsi
Kishore D
Contributor
January 22, 2023

Hi @Vamsi Kandala ,

Thanks for sharing code.  I managed with below code

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

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

But, here i want to add some other part like,
I will put total length of filed as 256. And the field  should allow multiple values with "|" as value separator.
so my field should allow like custom_filed112=1234|56|789|23456

if possible, can you help with this behavior?

Like sneha tangi likes this
Vamsi Kandala
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.
January 25, 2023

Hi @Kishore D

Can you try using '[0-9|]{1,10}'?

Note the pipe character '|' after the digit '9'.

Thanks,
Vamsi

sneha tangi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 15, 2023

Thanks @Kishore D 

Suggest an answer

Log in or Sign up to answer