-1

I'm using vee-validate and v-mask on this input

               <ValidationProvider
                  name="Credit Card Number"
                  rules="required"
                  mode="eager"
                  v-slot="{ errors }"
                >
                  <input class="default-input" type="text" name="Credit Card Number" id="cardNumber" placeholder="4567 1234 5678 9012" v-model="cardNumber" v-mask="'#### #### #### ####'" @keyup="handleCreditCard()">
                  <span class="text-red-500 mt-2">{{ errors[0] }}</span>
                </ValidationProvider>

and I'm getting the credit card number (including the spaces with the v-mask) on the v-model.

onKeyup I'm trying to match with the regex to display the credit card badge:

handleCreditCard(){

      //Visa
      if (/^4[0-9]{12}(?:[0-9]{3})?$/.test(this.cardNumber)){
        this.isVisa = true
        this.isAmex = false
        this.isMasterCard = false
      }
      //Amex
      if (/^^3[47][0-9]{13}$/.test(this.cardNumber)){
        this.isVisa = false
        this.isAmex = true
        this.isMasterCard = false
      }
      //Mastercard
      if (/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/.test(this.cardNumber)){
        this.isVisa = false
        this.isAmex = false
        this.isMasterCard = true
      } 
    }

But I can't make the badges to be displayed at the moment.

mt Data() has all set to false by default

data(){
  return {
    isVisa: false,
    isMasterCard: false,
    isAmex: false,
  }
}

Got the regex from here - https://www.regular-expressions.info/creditcard.html

Fabio Zanchi
  • 924
  • 3
  • 16
  • 32

1 Answers1

0

Removing the mask I can display the badge and using .replace() I can still show the input value on the other div with the spaces:

this.cardNumber.replace(/(.{4})/g, "$1 ")

Fabio Zanchi
  • 924
  • 3
  • 16
  • 32