Is it possible to limit the number of digits in a custom number field

Tal Rotem July 25, 2013

1. I would like to keep the custom field limited to 1 digit (e.g. a number between 0 and 9) with no decimal point. is it possible?

2. currently the field is shown with a very long edit box. can I shorten also the edit box size to indicate that only a small value is expected?

7 answers

1 accepted

1 vote
Answer accepted
RambanamP
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.
July 26, 2013

as you said if you are expecting decimal value 1 to 9 and field limit is 1 digit then create a select list and add 1 o 9 values as a options. so can able to select only those values.

i think this is what you are expecting!!!

RambanamP
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.
July 28, 2013

i think using javascript you can achive!!

Tal Rotem July 28, 2013

Thanks guys, but i was actually looking for something simpler, which is some way to set attributes for the existing field.

for example:

1. configure that this field has only one digit after the decimal point (e.g. nnn.x)

2. configure a valid range of numbers (min/max) for the input.

3. show a smaller edit box of 5 charachters istead of a full line.

So nothing of the above comes out of the box? or is there some simple way to get there?

Tal Rotem July 29, 2013

Thanks a lot ! This is the right direction - already managed to change the edit box size.

Can you please indicate how can I change the style of the title of the field (i.g. thegetElementById("customfield_12345") returns the field itself, not its title element.

Tal Rotem July 29, 2013

Hey, found it myself. This is a way to get the ID of the label used a the caption of a custom field/control:

function findLabelByControlName(ctr) {
   
   labels = document.getElementsByTagName('label');
   for( var i = 0; i < labels.length; i++ ) {
      //if (i<20) alert (labels[i].htmlFor);
      if (labels[i].htmlFor == ctr)
           return labels[i];
   }
 }// end of Function ()

RambanamP
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.
July 29, 2013

to change the style of the style of the title you can do it simple way,

for example to change color of label text, i used this

AJS.$('label[for=customfield_11329]').css({backgroundColor:'#red'});

just change on css if you want to change some thing

1 vote
Andrey Markelov
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.
September 4, 2013
0 votes
Tal Rotem August 3, 2013

Thanks for all the helpul answers above.

In addition to the above, here is the way I applied a simple validation, applied once the "update" button of the form is clicked (i.e. to submit):

btn = document.getElementById('edit-issue-submit');
  
   btn.onclick = function ()
    {
     if (condition) 
          alert ("Value must be a number between 0 and 9");
    }

if the button is note "Update" but "Create", the the button name should be 'create-issue-submit' in the code above

0 votes
ashwinirghure July 29, 2013

You can set the maxlength attribute to the existing customfield by using javascript.

AJS.$('#customfield_id').attr('maxlength','7')

Alexej Geldt
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.
July 29, 2013

cant you just add maxlength attribute to vm template?

<input .... maxlength="7" />

0 votes
JamieA
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.
July 29, 2013

You can do a simple validator with script runner, asserting the input matches a regex.

0 votes
Alexej Geldt
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.
July 29, 2013

One approach to restrict a text field to specific characters or specific length would be using javascript. Something like

AJS.$(function() {
    AJS.$(document).ready(function(){
        var textInput = AJS.$("#textInputElement");
        textInput.onkeydown = textInput.onkeyup = textInput.onblur = function() {
             textInput.value = textInput.value.replace(/[^0-9]+/, "");
        }
    });
});

example above removes everything from text input element that is not matching numbers 0-9 as you type.

refer to for further info http://stackoverflow.com/questions/3764821/best-way-to-restrict-a-text-field-to-numbers-only

Note: This should work everywhere but in Dialogs. Because elements that are rendered in dialogs are not present when DOM is build up. They are added to the DOM when the Dialog appears. Hence textInput in example above would not be found on document.ready event and therefore the script not applied.

You can however catch the JIRA.Events.NEW_CONTENT_ADDED event. It is fired when something is added to the DOM, such as a new Dialog. So if you want javascript to be applied to elements inside Dialogs. You would bind your function to new content added event. Example

AJS.$(function() {
	
    // execute on popup
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e, context, reason) {
    	 var $context = AJS.$(context);	
    	 
    	 // dialog appeared 
    	 if(reason == JIRA.CONTENT_ADDED_REASON.dialogReady){
    		 
    		 // get elements. notice: context gives you an array of elements. if you are sure that there is only one - use $context.find(#inputElement")[0];
    		 var inputElements = $context.find("#inputElement");
                 
                 // ... from here do what you want with inputElement
	 
    	 }
    });
});

0 votes
Florin Manaila
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.
July 25, 2013

You'll have to write a small plugin for that. It shouldn't be hard since all you have to do is extend the existing NumberCFType and add a validation and a custom view.

Tal Rotem July 28, 2013

Thanks a lot ! This is the right direction - already managed to change the edit box size.

Can you please indicate how can I change the style of the title of the field (i.g. the getElementById("customfield_12345") returns the field itself, not its title element.

Suggest an answer

Log in or Sign up to answer