Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to apply mask to string value not element #802

Open
JoshKasap opened this issue Oct 24, 2023 · 0 comments
Open

How to apply mask to string value not element #802

JoshKasap opened this issue Oct 24, 2023 · 0 comments

Comments

@JoshKasap
Copy link

All the examples and use cases seem to be on how to use the plugin to apply a mask to the value of an input field. However, I've run into a situation where I need to apply the mask to a raw string value and get the resulting mask.

for example I currently have this custom mask

function getPhoneMask (input, e, field, options){
    let unmaskedVal = input.replace(/[^\d]/g, '');
    let masks = ['(000) 000-00000000000', '+0 (000) 000-00000000', '+00 (000) 000-0000000', '+000 (000) 000-000000', '+0000 (000) 000-00000', '+00000 (000) 000-0000'];
    let mask = unmaskedVal.length == 15 ? masks[5]: unmaskedVal.length == 14 ? masks[4] : unmaskedVal.length == 13 ? masks[3] : unmaskedVal.length == 12 ? masks[2]: unmaskedVal.length == 11 ? masks[1] : masks[0];

    console.log(`mask: ${mask}`);
    return mask
}

$('.phoneMask').mask(getPhoneMask, {
    onKeyPress: function(input, e, field, options) {
        let mask = getPhoneMask(input, e, field, options);
        field.mask(mask, options);
    }
});

This finds the value of each element with the .phoneMask class and applies the mask on load and on keypress.

however, let say i have a random string "1231231234" This value is not the value of an input element but just the value of some variable in my code. How can i apply my custom mask just to this value so that i would end up with (123) 123-1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant