[Input Masks] Decimal Separator ',' problem

[Input Masks] Decimal Separator ',' problem

  
Forge Component
(20)
Published on 2012-04-27 by António Chinita
20 votes
Published on 2012-04-27 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.
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.
Bump... :)
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? 
Hi Steve,

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

I'll attach my eSpace for you.
Perfect that worked great, thanks very much Victor.

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.

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.