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

No documentation on how to use custom tokens with directive #48

Open
Vlaoff opened this issue Dec 7, 2017 · 8 comments
Open

No documentation on how to use custom tokens with directive #48

Vlaoff opened this issue Dec 7, 2017 · 8 comments

Comments

@Vlaoff
Copy link

Vlaoff commented Dec 7, 2017

Hi

I could not find how to use custom tokens with the directive.

Edit: Watching how its done in the component I figured it out, but adding it to the documentation could save time to others in the future.

You just need to pass an object to the v-mask

<input type="tel" v-mask="{mask: '+7 (F##) ###-##-##', tokens: hexTokens}" v-model="phone"/>

You could also define it as an object

data () {
    customMask: {
      mask: '+7 (F##) ###-##-##',
      tokens: 
        'F': {
          pattern: /(?!8)\d/
        },
        '#': {pattern: /\d/}
      }
    }
  }
<input type="tel" v-mask="customMask" v-model="phone"/>
@ankology
Copy link

ankology commented Sep 6, 2018

If you need to use a existing token, is required to pass again with your custom tokens.

@marcosrocha85
Copy link

marcosrocha85 commented Oct 10, 2018

I spent a lot of time until reach here. Despite this project is widely be used, it should be merged and have a proper attention.

Anyway, @Vlaoff or @ankology . Can you show me a more detailed example how did you done? I'm using ES5 and didn't manage to make custom mask work. I need letters to be uppercased when typed.

var app = new Vue({
    el: "#my_application",
    data: {
        customTokens: {
            mask: 'CCCCC',
            tokens: {
                'C': {
                    pattern: /[0-9a-zA-Z]/,
                    transform: function(v) {
                        return v.toLocaleUpperCase();
                    }
                }
            }
        },
    }
});

<input v-mask="customTokens" v-model="license_plate"/>

@ankology
Copy link

I didn't run the code, @marcosrocha85 but your input is type="tel" maybe is it the issue? The rest looks ok.

@marcosrocha85
Copy link

Thank you for your reply. Actually the type="tel" was a typo. But I solved by doing 'AAA-#X##' at v-mask property and converting to UpperCase at server so I can type ABC-1234 and ABC-1T34 which are two possible license plates of Southern Common Market.

@vlad909
Copy link

vlad909 commented Oct 16, 2018

@marcosrocha85 but this example doesn't work :D

@marcosrocha85
Copy link

@vlad909 It was my attempt and stuck at that kind of "code". Anyway, thank you guys for your attention.

@kennerus
Copy link

kennerus commented Mar 29, 2019

I had same problem as you. Needed to add custom regexp. So here is my solution for custom tokens:
https://codesandbox.io/s/7mw8x06pv1?fontsize=14

I first added just one token and it was like:

data() {
    return {
      customTokens: {
        'Y': {pattern: /[0-9]/}
      },
    }
  }

But it owerrites the-mask tokens, so i needed to add to ":tokens" all tokens that i want to use (just copied from source code all tokens).

@huynhquangminh
Copy link

I spent a lot of time until reach here. Despite this project is widely be used, it should be merged and have a proper attention.

Anyway, @Vlaoff or @ankology . Can you show me a more detailed example how did you done? I'm using ES5 and didn't manage to make custom mask work. I need letters to be uppercased when typed.

var app = new Vue({
    el: "#my_application",
    data: {
        customTokens: {
            mask: 'CCCCC',
            tokens: {
                'C': {
                    pattern: /[0-9a-zA-Z]/,
                    transform: function(v) {
                        return v.toLocaleUpperCase();
                    }
                }
            }
        },
    }
});

<input v-mask="customTokens" v-model="license_plate"/>

It is run with this code

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

6 participants