Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleave.js Phone CA

I'm trying to format a phone number field using Cleave.js and it's not working but I can't seem to figure out why.

I started off like this:

import Cleave from 'cleave.js';

var cleave = new Cleave('.phone', {
    'phone': true,
    'phoneRegionCode': 'CA'
});

And I got this error:

> Uncaught Error: [cleave.js] Please include phone-type-formatter.{country}.js lib

So I imported the library that I was missing by adding:

import CleavePhone from 'cleave.js/dist/addons/cleave-phone.ca';

So now my code is:

import Cleave from 'cleave.js';
import CleavePhone from 'cleave.js/dist/addons/cleave-phone.ca';

var cleave = new Cleave('.phone', {
    'phone': true,
    'phoneRegionCode': 'CA'
});

I'm not getting any console errors anymore, but the formatting of the phone number doesn't seem to be working at all.

I tried to change the options of the Cleave to a date:

var cleave = new Cleave('.phone', {
    'date': true,
    'datePattern': ['Y', 'm', 'd']
});

And the input seems to format properly. Therefore, I know my query selector for the element is okay.

But I can't seem to figure out why my phone number is not formatting properly.

like image 635
Chin Leung Avatar asked Mar 13 '17 17:03

Chin Leung


1 Answers

Instead of importing, try requiring: require('cleave.js/dist/addons/cleave-phone.ca')

like image 181
Jeffrey Brayne Avatar answered Oct 19 '22 16:10

Jeffrey Brayne