Weight mask

Asked

Viewed 1,480 times

3

Searching the internet, I found a code that masks weight in the field. But this code works with ID criteria.

I would like it to work with CLASS, to be able to use in other fields.

function id(el){
        return document.getElementById(el);
}
window.onload = function(){
        id('peso').onkeyup = function(){
            var v = this.value,
                integer = v.split('.')[0];


            v = v.replace(/\D/, "");
            v = v.replace(/^[0]+/, "");

            if(v.length <= 3 || !integer)
            {
                if(v.length === 1) v = '0.00' + v;
                if(v.length === 2) v = '0.0' + v;
                if(v.length === 3) v = '0.' + v;
            } else {
                v = v.replace(/^(\d{1,})(\d{3})$/, "$1.$2");
            }

            this.value = v;
        }
};

3 answers

3


I imagine your problem includes dynamically Genaro fields, and hence you need to use delegation. My suggestion is:

function mascara() {
    var v = this.value, integer = v.split('.')[0];
    v = v.replace(/\D/, "");
    v = v.replace(/^[0]+/, "");

    if (v.length <= 3 || !integer) {
        if (v.length === 1) v = '0.00' + v;
        if (v.length === 2) v = '0.0' + v;
        if (v.length === 3) v = '0.' + v;
    } else {
        v = v.replace(/^(\d{1,})(\d{3})$/, "$1.$2");
    }

    this.value = v;
}
$('#elementoPai').on('keyup', '.peso', mascara);

If you add more information about how you want to format the output we can help you adapt the mask.

1

In place of

function id(el){
    return document.getElementById(el);

}

Place

function id(el){
   return document.getElementsByClassName(el);
}

And puts a class with the name 'weight' in its element.

  • .getElementsByClassName() gives a list/array. Just as this will give error... you must have at least [0], ie return document.getElementsByClassName(el)[0];

1

You can directly use jQuery for this if you are not using dynamically generated fields:

$('.peso').keyup(function () {
    var v = this.value,
        integer = v.split('.')[0];

    v = v.replace(/\D/, "");
    v = v.replace(/^[0]+/, "");

    if (v.length <= 3 || !integer) {
        if (v.length === 1) v = '0.00' + v;
        if (v.length === 2) v = '0.0' + v;
        if (v.length === 3) v = '0.' + v;
    } else {
        v = v.replace(/^(\d{1,})(\d{3})$/, "$1.$2");
    }

    this.value = v;
});

I also suggest changing the line

v = v.replace(/\D/, "");

for

v = v.replace(/\D/g, "");

To correctly remove the letters.

  • This post was blocked by you, but it has already been solved and updated for better viewing, but it is still blocked, how to solve? http://answall.com/questions/63821/calculo-de-cubagem-com-jquery/63831

  • I didn’t block the post, I’m not allowed to do that. I just voted, along with other users, to keep the question closed, because at the time I entered the evaluation queue I had no accepted answer, the text of the question was not clear and I noticed that you opened another question for the same topic: http://answall.com/questions/63849/calcular-valores-de-v%C3%A1rios-campos . If you want to reopen the question, edit it and it will fall into the revision queue for reopening.

  • It’s already been changed, not the original plus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.