406
Views
7
Comments
[Input Masks] Decimal Separator ',' problem
Question
input-masks
Web icon
Forge asset by António Chinita
Antonio,
 
I had some problems using the input mask when decimal separator was ',' like it is in Brazil.
 
When the server sent to the html page the value '150' the mask showed the value '1,50', because of the two decimal numbers.
 
To solve the problem I add this javascript code before the plug-in apply the mask:
 
if('" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator + "' == ',') {
    if(osjs('#" + InputId + "').val().indexOf('.') == -1){
        osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '.00');
    } else {
        if(osjs('#" + InputId + "').val().substring(osjs('#" + InputId + "').val().indexOf('.') + 1).length < 2) {
            osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '0')
        }
    } 
}      
 
 
This javascript adds '.00' when the value does not contain any decimal value.
 
I hope it helps someone else.
 
thanks.
2020-12-07 17-35-54
António Chinita
Thanks for your input Victor. I never thought of that specific situation.
I'll implement a way to handle such situations as well as other fixes soon. :)

Thanks a lot.
2014-09-04 01-10-12
Mark Bayles
Bump... :)
2014-01-24 14-08-25
Steve Dalgleish
Could I just clarify where you added this?  I'm looking in the Mask webblock in the InputMask espace.   I'm adding a new script block in the IsCurrency If container.  Is that correct? 
2023-05-03 10-22-17
Victor Salvalagio Pereira
Hi Steve,

That's correct. You have to add the script in the "true" condition.

I'll attach my eSpace for you.
InputMask.oml
2014-01-24 14-08-25
Steve Dalgleish
Perfect that worked great, thanks very much Victor.
2018-08-02 14-20-10
Bruno Domingues

Hello,

Recently i have a problem with this extension when i put 3 decimals on mask.

The Problem his when i put the number 0.100 the result is 0.010. The Javascript code cut one zero on the right.

To solve this problem i wrote this condition on the top:

SyntaxEditor Code Snippet

  if('" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals +"' == 3) {
                if(osjs('#" + InputId + "').val().indexOf('.') == -1){
                    osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '.000'); 
                } else {
                    if(osjs('#" + InputId + "').val().substring(osjs('#" + InputId + "').val().indexOf('.') + 1).length < 3) {
                        osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '0')
                    } 
                }  

    } else {
                if(osjs('#" + InputId + "').val().indexOf('.') == -1){
                    osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '.00'); 
                } else {
                    if(osjs('#" + InputId + "').val().substring(osjs('#" + InputId + "').val().indexOf('.') + 1).length < 2) {
                        osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '0')
                    } 
                }  
    }


I hope to help some one with this.

Thanks.

2018-08-02 14-20-10
Bruno Domingues

There is a better solution for any decimals. This code below should be placed in the JavaScript Tag, 

SyntaxEditor Code Snippet

"<script type=""text/JavaScript"">
    osjs(document).ready(function () {
        " + If(InputId <> "",
"  
        var decimals = '.';
        var i = 0;
        while (i < '" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals +"') {
            decimals += '0';
            i++;
        }

if('" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator + "' == ',') {
       
        if(osjs('#" + InputId + "').val().indexOf('.') == -1){
            osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + decimals); 
        } else {
                while (osjs('#" + InputId + "').val().substring(osjs('#" + InputId + "').val().indexOf('.') + 1).length < '" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals +"'){
                    osjs('#" + InputId + "').val(osjs('#" + InputId + "').val() + '0');
                } 
        }  
    
}

osjs('#" + InputId + "').before(osjs('#" + InputId + "').clone().attr('id','" + InputId + "' + '_mask')); osjs('#" + InputId + "' + '_mask').attr('name',osjs('#" + InputId + "' + '_mask').attr('name') + '_mask'); osjs('#" + InputId + "').css('display','none');

if('" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator + "' == ',') {

        if(osjs('#" + InputId + "_mask').val().indexOf('.') == -1){
            osjs('#" + InputId + "_mask').val(osjs('#" + InputId + "_mask').val() + decimals);
        } else {
            while (osjs('#" + InputId + "_mask').val().substring(osjs('#" + InputId + "_mask').val().indexOf('.') + 1).length < '" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals            +"') {
                osjs('#" + InputId + "_mask').val(osjs('#" + InputId + "_mask').val() + '0');
                } 
        } 
    
}   

osjs('#" + InputId + "_mask').keyup(function () { osjs('#" + InputId + "').val(osjs(this).val().replace(/\"+CurrencyMaskOptions.CurrencyMaskOptions.GroupSeparator+"/g,'').replace(/\" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator +"/g,'.'));  });

osjs('#" + InputId + "_mask').maskMoney({thousands:'" + CurrencyMaskOptions.CurrencyMaskOptions.GroupSeparator + "', decimal:'" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator +"', precision:" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals + ", allowZero:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.AllowZero) + ", allowNegative:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.AllowNegative) + ", defaultZero:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.DefaultZero) + ", showSymbol:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.ShowSymbol) + ", symbol:'" + CurrencyMaskOptions.CurrencyMaskOptions.Symbol + "', symbolStay:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.PersistentSymbol) + "});
         osjs('#" + InputId + "_mask').trigger('mask');","") +
        If(ClassToMask <> "",
"        osjs('." + ClassToMask + "').maskMoney({thousands:'" + CurrencyMaskOptions.CurrencyMaskOptions.GroupSeparator + "', decimal:'" + CurrencyMaskOptions.CurrencyMaskOptions.DecimalSeparator +"', precision:" + CurrencyMaskOptions.CurrencyMaskOptions.Decimals + ", allowZero:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.AllowZero) + ", allowNegative:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.AllowNegative) + ", defaultZero:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.DefaultZero) + ", showSymbol:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.ShowSymbol) + ", symbol:'" + CurrencyMaskOptions.CurrencyMaskOptions.Symbol + "', symbolStay:" + ToLower(CurrencyMaskOptions.CurrencyMaskOptions.PersistentSymbol) + "});
        osjs('." + ClassToMask + "').trigger('mask');","") +
"    });
</script>"


I hope to help someone whit this code.

Thanks.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.