Convert Positive Number to Negative Number

Hi,

I have a number field which forces negative values but when I type a positive number it rounds the number to the nearest threshold I have set.

I have to manually enter the negative sign from the keyboard in order for the field to accept a negative number. Is there no way for it to dynamically set the value to a negative number?

Here is my code:

//This code will show/hide the Holiday Hours field
//Hides field on Load
$(fd.field('Holiday_Hours').$parent.$el).hide();
function ShowHideHolidayHours(){
    if (fd.field('Annual_Leave_Taken').value == 'Same as'){
    //Sets value in Holiday Hours field to 0
    fd.field('Holiday_Hours').value = 0;
    $(fd.field('Holiday_Hours').$parent.$el).hide();
    $(fd.field('RHolidayHours').$parent.$el).show();
    //Setting border-bottom to green upon selection
    $($(fd.field('Annual_Leave_Taken').$el).find('span')[0]).attr('style', 'border-bottom:3px solid #6CEE5A;');
    }
    
    //Shows Holiday Hours field, renames field 'holiday hours overpaid' & applies threshold
    if (fd.field('Annual_Leave_Taken').value == 'More than'){
    $(fd.field('Holiday_Hours').$parent.$el).show();
    fd.field('Holiday_Hours').validate();
    $(fd.field('RHolidayHours').$parent.$el).show();
    fd.field('Holiday_Hours').clear();
    fd.field('Holiday_Hours').title = 'Holiday Hours Overpaid';
    fd.field('RHolidayHours').title = 'Holiday Hours Overpaid';
    //Setting border-bottom to green upon selection
    $($(fd.field('Annual_Leave_Taken').$el).find('span')[0]).attr('style', 'border-bottom:3px solid #6CEE5A;');
    //This will set the number format & threshold 
    fd.field('Holiday_Hours').widgetOptions = {
        format:"n2",
        min: 1,
        max: 100,
        decimals:2,
        round:false
        }
    }
  
    //Shows Holiday Hours field, renames field 'holiday hours due' & applies threshold
    if (fd.field('Annual_Leave_Taken').value == 'Less than'){
    $(fd.field('Holiday_Hours').$parent.$el).show();
    fd.field('Holiday_Hours').validate();
    $(fd.field('RHolidayHours').$parent.$el).show();
    fd.field('Holiday_Hours').clear();
    fd.field('Holiday_Hours').title = 'Holiday Hours Due';
    fd.field('RHolidayHours').title = 'Holiday Hours Due';
    //Setting border-bottom to green upon selection
    $($(fd.field('Annual_Leave_Taken').$el).find('span')[0]).attr('style', 'border-bottom:3px solid #6CEE5A;');
    //This will set the number format & threshold 
    fd.field('Holiday_Hours').widgetOptions = {
        format:"n2",
        min: -100,
        max: -1,
        decimals:2,
        round:false
        }
    }
};
//calling function on change of Entitlement
fd.field('Annual_Leave_Taken').$on('change', ShowHideHolidayHours);

Thanks!

UPDATE

I think I found a solution to this problem but I don't know how to setup the syntax to make it work...

I read somewhere that using -Math.abs() will help negate the value that users enter in the field...

I know the below function isn't the correct syntax but this should help resolve my issue.

function ShowHideHolidayHours(value){
//Shows Holiday Hours field, renames field 'holiday hours due' & applies threshold
        if (fd.field('Annual_Leave_Taken').value == 'Less than'){
	return fd.field('Holiday_Hours').value = -Math.abs(value)
     }
}

Any guidance or help is appreciated!

Hello @DryChips,

Setting the Min/Max properties, won' allow you to change the positive value to negative. Remove this part of the code.

And add the code below to allow users to enter only negative values within the range:

fd.field('Field1').$on('change', function(value){
    console.log(value)
    if(value > 0 && value < 100){
        fd.field('Field1').value = -value
    }
    if(value> 100) {
        fd.field('Field1').value = -1
    }
    if(value < -100) {
        fd.field('Field1').value = -100
    }
})
1 Like

Thank you, how can I use this for only when the selection is 'Less than'? I put this code in and it applies this to only this field and not based off a selection.

I have written this code but its not working:

function limit() {
    if (fd.field('Annual_Leave_Taken').value == "Less than"){
        if (fd.field('Holiday_Hours').value > 0 && fd.field('Holiday_Hours').value < 100){
        fd.field('Holiday_Hours').value = -value
        }
    }
        if (fd.field('Annual_Leave_Taken').value == "Less than"){
        if (fd.field('Holiday_Hours').value > 100){
        fd.field('Holiday_Hours').value = -1
        }
    }
        if (fd.field('Annual_Leave_Taken').value == "Less than"){
        if (fd.field('Holiday_Hours').value < -100){
        fd.field('Holiday_Hours').value = -100
        }
    }
}
fd.field('Annual_Leave_Taken').$on('change', limit);

@DryChips,

You need to paste the code within one condition. There is no need in duplicating it:

function limit() {
    if (fd.field('Annual_Leave_Taken').value == "Less than"){
        if (fd.field('Holiday_Hours').value > 0 && fd.field('Holiday_Hours').value < 100){
            fd.field('Holiday_Hours').value = -value
        }
    
        if (fd.field('Holiday_Hours').value > 100){
            fd.field('Holiday_Hours').value = -1
        }
    
        if (fd.field('Holiday_Hours').value < -100){
            fd.field('Holiday_Hours').value = -100
        }
    }
}

Hi Margarita,

Its fine. I figured it out.

Here is the code that resolved my issue:

//This code will convert a positive number to a negative number
fd.field('Holiday_Hours').$on('change', function(value){
    if (fd.field('Annual_Leave_Taken').value == "Less than"){
        //Logs the value to console
        console.log(value)
        //Checks value entereed in threshold and negates it
        if(value > 0 && value < 100){
        fd.field('Holiday_Hours').value = -value
        }
        if(value> 100) {
        fd.field('Holiday_Hours').value = -1
        }
        if(value < -100) {
        fd.field('Holiday_Hours').value = -100
        }
    }
})

Thank you for your support! Much appreciated.

1 Like

Hi margarita,

I have a small problem,

I am validating the same field and I would like the error message to be tailored to the condition I have set but now I am seeing two types of errors in the field:

Here is my code:

//This code will validate the Holiday Hours field
fd.field("Holiday_Hours").validators.push({
    name:"",
    error:"Please enter a value between -1 and -307.2",
    validate: function(value){
       if(fd.field("Annual_Leave_Taken").value == "Less than"){
           console.log(value)
           if(value > -1 || value < -307.2){
           return false
           }
           return true
       }
    }
})

//This code will validate the Holiday Hours field
fd.field("Holiday_Hours").validators.push({
    name:"",
    error:"Please enter a value between 1 and 307.2",
    validate: function(value){
       if(fd.field("Annual_Leave_Taken").value == "More than"){
           console.log(value)
           if(value < 1 || value > 307.2){
           return false
           }
           return true
       }
    }
})

@DryChips,

No need to add two separate validations for the field. You need to combine this into one validation like this:

//This code will validate the Holiday Hours field
fd.field("Holiday_Hours").validators.push({
    name:"",
    error:'Enter value',
    validate: function(value){
       if(fd.field("Annual_Leave_Taken").value == "Less than" && value){
           console.log(value)
           if(value > -1 || value < -307.2){
               this.error = "Please enter a value between -1 and -307.2"
           return false
           }
           return true
       }       
        
        if(fd.field("Annual_Leave_Taken").value == "More than" && value){
           console.log(value)
           if(value < 1 || value > 307.2){
               this.error = "Please enter a value between 1 and 307.2"
           return false
           }
           return true
       }
    }
})
1 Like

Thank you! This solved my issue. :smiley:

1 Like