question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Phone/Numeric mask adds extra digit after blur / Using prefix prevents user from selecting all & deleting

See original GitHub issue

Hi all,

I love Cleave, but I’ve always had this issue when trying to add masks to phone numbers. To recreate the issue, I’ve setup a Codepen here, I’m using Cleave with Vue:

https://codepen.io/MatrixClaw/pen/JjYXqKb

The first input uses the US region phone configuration:

{
    phone: true,
    phoneRegionCode: 'US'
}

The second uses a custom mask to add parentheses and dashes to the phone number:

{
    numericOnly: true,
    prefix: '(',
    delimiters: [') ', '-'],
    blocks: [4, 3, 4],
    noImmediatePrefix: true
}

When I type in a phone number, Cleave correctly masks the input according to the rules, but then on blur, if you’ve over-typed, it adds an extra digit at the end.

Alternatively, if you try to select everything in the custom mask field and delete it, nothing happens (this seems to only happen if you use a prefix). You have to delete each character one by one. I’ve added a third example removing the prefix and adding it as a delimiter to demonstrate:

{
    delimiters: ['(', ') ', '-'],
    blocks: [0, 3, 3, 4],
    numericOnly: true
}

Here’s a short video for clarity:

https://video.drift.com/v/abylKah1gX7/

Any ideas what’s going on here??

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
KaliaJScommented, Jul 29, 2021

@calebwaldner Glad I was able to help someone. Here is the version for Vue3 :

import Cleave from 'cleave.js'

export default {
  mounted: (element, { value }) => {
    const input = element.tagName === 'INPUT' ? element : element.querySelector('input')
    input.cleave = new Cleave(input, value || {})
  },
  updated: (element) => {
    const input = element.tagName === 'INPUT' ? element : element.querySelector('input')
    const event = new Event('input', { bubbles: true })
    requestAnimationFrame(() => {
      input.value = input.cleave.properties.result
      input.dispatchEvent(event)
    })
  },
}
1reaction
calebwaldnercommented, Jul 29, 2021

@DinsmoreDesign @demyan1411 I found the solution. You need to change the directive like this :

  inserted: (el, binding) => {
    const element = el.tagName === 'INPUT' ? el : el.querySelector('input')
    element.cleave = new Cleave(element, binding.value || {})
  },
  update: (el) => {
    const element = el.tagName === 'INPUT' ? el : el.querySelector('input')
    const event = new Event('input', { bubbles: true })
    requestAnimationFrame(() => {
      element.value = element.cleave.properties.result
      element.dispatchEvent(event)
    })
  }

Tell me if it’s working on your side.

We were having the same issue. I added these to the directive and it seemed to fix things on my end. Thanks @KaliaJS

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the value without the mask characters? · Issue #109
I'm hesitant to add this feature to the components or core itself yet because simply stripping out the mask would not satisfy all...
Read more >
AutoCompleteTextView - Android Developers
The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content...
Read more >
How to Mask Hair and Blur or Change Backgrounds in ...
This tutorial will show you how to easily mask around a subject's hair in Photoshop. Once the selection around the subject is complete, ......
Read more >
Ext.form.field.Number | Ext JS 6.2.0 - Sencha Documentation
An optional extra CSS class that will be added to this component's Element. ... true to prevent the user from changing the field,...
Read more >
GXP2130/GXP2140/GXP2160/GXP2170/GXP2135 Enterprise ...
Please do not use a different power adaptor with devices as it may cause ... Added an option on web UI to warm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found