Giter VIP home page Giter VIP logo

vue-phone-number-input's Introduction

vue-phone-number-input

license vue 2 npm npm Codacy grade

npm

A beautiful text field to format phone numbers made with VueJS


⚠️ This plugin is unmaintained ⚠️

Version for Vue 2 on maz-ui v2.x

The version for Vue 2 is available here: maz-ui v2.x - MazPhoneNumberInput

New version for Vue 3 on maz-ui v3.x

The version for Vue 3 is available here: maz-ui v3.x - MazPhoneNumberInput


vue-phone-number-input

Demo

Enjoy

Installation

Using yarn

yarn add vue-phone-number-input

Using npm

npm i --save vue-phone-number-input

Usage

ES6 Modules / CommonJS

import VuePhoneNumberInput from 'vue-phone-number-input';
import 'vue-phone-number-input/dist/vue-phone-number-input.css';

Vue.component('vue-phone-number-input', VuePhoneNumberInput);
<VuePhoneNumberInput v-model="yourValue" />

UMD

<VuePhoneNumberInput v-model="yourValue" />

<script src="https://unpkg.com/vue" charset="utf-8"></script>
<script src="./dist/vue-phone-number-input.umd.min.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="./dist/vue-phone-number-input.css">

<script type="text/javascript">
  Vue.component('vue-phone-number-input', window.VuePhoneNumberInput.default);
</script>

Features List

  • You can set preferred-countries, ignored-countries or have only-countries
  • Validator State: input becomes green (you can modify this color with valid-color option) when the phone number is valid (can be disabled by no-validator-state attr)
  • Multi options to getting country code : By default the component get the country code via the browser (disable it with no-use-browser-locale) or you can use fetch-country to get the country code via https://ip2c.org/s (network needed) - you can use default-country-code option instead to set one
  • Phone number formatting while typing
  • You can search your country in list (open countries list & type your country name)
  • Keyboard accessibility (Arrow down, Arrow up: Countries list navigation - Escape: Close countries list)
  • Phone number example for each country in placeholder/label
  • Auto focus phone number input after selecting country
  • You can disable the flags - no-flags props
  • Set your translations

All options of VueInputUi are available

  • Differents size of input (sm or lg) size="sm|lg"
  • Disabled option (disabled prop)
  • Dark UI option (dark prop)
  • Active a clear button by the prop clearable (cf: VueInputUi options)
  • Active a loader progress bar by the prop loader (cf: VueInputUi options)
  • And others

Props API

Props Type Required Default
v-model String/Int true -
id String false VuePhoneNumberInput
color String HEX no dogderblue
valid-color String HEX no yellowgreen
error-color String HEX no orangered
size String `sm lg` no
default-country-code (1) String no null
preferred-countries (2) Array<string> no null
ignored-countries Array<string> no null
only-countries Array<string> no null
no-validator-state Boolean no false
no-flags Boolean no false
disabled Boolean no false
dark Boolean no false
dark-color String (hex) no #424242
required Boolean no false
error Boolean no false
clearable Boolean no false
loader (3) Boolean no false
translations (4) Object no null
countries-height (5) Number no 30
no-use-browser-locale (6) Boolean no false
fetch-country (7) Boolean no false
no-country-selector (8) Boolean no false
border-radius Number no 4
show-code-on-list Boolean no false
no-example Boolean no false

(1) Ex : default-country-code="FR"

(2) Ex : preferred-countries="['FR', 'BE', 'DE']" This countries will be at the top of the list

(3) Loader progress bar has the input color (color props)

(4) translations comes to replace default texts - Ex :

translations="{
  countrySelectorLabel: 'Code pays',
  countrySelectorError: 'Choisir un pays',
  phoneNumberLabel: 'Numéro de téléphone',
  example: 'Exemple :'
}"

(5) height in px of the rows included in the dropdown. Ex: countries-height: 40

(6) By default the component get country code via browser - No network needed but not work on SSR with NuxtJS (disable it with no-use-browser-locale)

(7) Fetch country code via https://ip2c.org/s - Network needed - (Do not use it with default-country-code options)

(8) The country selector is not shown, you can validate your phone number with the country code set

Events API

Event Return
phone-number-focused - (emit when phone number input is focused)
phone-number-blur - (emit when phone number input is blur)
input AsYouType value (emit when new value is enter on phone number input && when a country is choosed)
update All values (cf values in table on demo) (emit when new value is enter on phone number input && when a country is choosed)

Keyboard accessibility

Props Action
ArrowDown Navigation down in countries list
ArrowUp Navigation up in countries list
Escape Close countries list
All letters characters Searching country name in countries list (should be open)

Named slots

Slot Action
arrow Override the default arrow character for toggling the list of countries

Contribution

Project setup

npm install

Compiles and hot-reloads for development

npm run serve

Lints and fixes files

npm run lint

License

This project is licensed under MIT License

vue-phone-number-input's People

Contributors

antoine92190 avatar infcesar avatar louismazel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-phone-number-input's Issues

I cannot import to my NUXT project

Here is my set up :

  1. npm i --save vue-phone-number-input

  2. create vue-phone-number-input.js to /plugins
    import VuePhoneNumberInput from "vue-phone-number-input" import "vue-phone-number-input/dist/vue-phone-number-input.css" Vue.component("vue-phone-number-input", VuePhoneNumberInput)

  3. add ~nuxt.config.js
    plugins: [ "~/plugins/vue-phone-number-input" ],

  4. in my component
    <VuePhoneNumberInput v-model="phone"/>

Vue warning :
[Vue warn]: Unknown custom element: <VuePhoneNumberInput> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

preferredCountries sort support

now preferredCountries sort is not support,

for example

if the given preferredCountries is ['FR', 'BE', 'DE']

the actual effect is ['BE', DE''FR', '], is sort by the first char.

Smaller css file

Component works well, but the css file is 435k. I think the main reason is the base64 images of the country flags. It's making my bundle size too large.

It would be great if you could use unicode country flags instead and get rid of the images -- http://xahlee.info/comp/unicode_flags.html

Class lm-text-danger stays after field is valid

I've set the :error-prop because I needed to do some custom validation, however I found that the label is still red even after the component has successfully validated the input.

Screen Shot 2019-11-14 at 13 15 58

I feel like .lm-text-danger should only be set if isValid is false and error is true, not just if error is true (I'm just making an assumption that that's how it's being done now). Or alternatively override the CSS for .lm-text-danger when is-valid is present (which is how I solved it).

Auto choise country

hello! nice plugin.

can I automatically set the country code when a user visits the site? I have an API to get the user's country. and I would like to automatically set the country based on the results of this api

Add class is-valid when a phone number is valid

I found myself adding a class to my wrapper component in order to apply "is valid" styles (by checking the data returned from update-event). Would prefer that the component itself added this class (semi related to #47 as this would likely clash with is-focused).

In IE 11, cannot change country code unless phone number is entered

A coworker of mine on Windows reported an issue with IE 11 and changing country, you can only change it once, then you're unable to unless you start typing a phone number.

While the phone number is present, you are able to change country freely like in any other browser, but removing it will return back to not being able to change country.

Did not report any console errors.

Passing v-model with country code fails

When you pass a phonenumber with country code the component doesn't behave like it should (Select correct country in list, strip countrycode and display correctly)

Unable to set placeholder

<VuePhoneNumberInput placeholder="Place Holder Text" ></VuePhoneNumberInput>

Unable to change the place holder attribute.
"version": "1.0.1",

Getting Wrong Country Code Selected

Description

Getting Wrong Country Code Selected, It is happened after some time When you start the play with phone number.

A clear and concise description of what the bug is.

Steps to reproduce

Example:

  1. Click on country list dropdown
  2. Start typing like sin, ind
  3. Select a country
  4. reopen country list by clicking on dropdown
  5. Select any country then you will not get expected result. like if you select India then the random code will reflect on page.

Expected behavior

Same country code should be displayed after selecting from dropdown

Screenshots

ezgif-4-cb0c6050c5e5

Device

  • OS: iOS
  • Browser: Chrome 75,

Argentina examples changes to 9

Hello, if you input a number like the example, the second character changes to a 9 automatically...

Try to input 011 15 or 011 11

Also i would like to change argentinian example label, is this possible?

Add required-attribute

The input element doesn't have the required-attribute, only aria-required, so right now this component does not support HTML5 form validation.

[FEATURE REQUEST] Port to NativeScript Vue

Is your feature request related to a problem? Please describe

I want to use a phone number input in NativeScript Vue but there is no such module.

Describe the solution you'd like

Maybe it would be possible to port this module to NativeScript. Most likely just the elements need to be changed (e.g. <div>, etc.)

Describe alternatives you've considered

There is no alternative yet.

[Feature Request] Add validation when users type letters

When individual types in letters or other characters except for numbers in the field:

Additional form validation should be in place, some suggestions:

  • the validation example should pop up already
  • there's a way to inform them that numbers only are allowed
  • No value is being added in the field and the validation label will pop up, informing them to that numbers are only allowed

Thanks and nice work on this component. :)

(FEATURE REQUEST) Support for Typescript

Is your feature request related to a problem? Please describe

Can't import the component in main.ts. Error:

Could not find a declaration file for module 'vue-phone-number-input'. Try `npm install @types/vue-phone-number-input` if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-phone-number-input';`

Describe the solution you'd like

Add declaration file and enable usage with Typescript.

Dark theme incompatibility with disabled

Dark theme works perfectly.
However, setting disabled prop to true disable the dark theme.

1

As quick and dirty fix, I use pointer-events: none to simulate a read only field:
2

Change country dinamically

Is there any way to change selected country after the component has been displayed?

I have default-country-code set to:

:default-country-code="phone_country"

When changing phone_country selected country is not changing in the form.

Infinite render loop under certain conditions

I am getting an infinite render loop using this component in Framework7 under certain conditions:

vue.runtime.esm.js?2b0e:619 [Vue warn]: You may have an infinite update loop in a component render function.

found in

---> <CountrySelector> at CountrySelector.vue
       <VuePhoneNumberInput> at index.vue
         <F7PageContent>
           <F7Page>
             <PhonePage> at src/components/PhonePage.vue
               <F7View>
                 <F7App>
                   <App> at src/App.vue
                     <Root>

The conditions are:

  1. vue-phone-number-input component is registered inline in the parent
  2. parent is routed page component module defined in SFC
    If these conditions are different (e.g., global registration, or declared as inline template), then it works fine.

I also observed that if comment out some of the code in CountrySelector's onFocus method, then the error does not occur, although the selector doesn't open either. It seems to be a focus-blur infinite loop, perhaps having to due with how the vue-click-outside directive is used, but I'm not sure.

Here is a repro repo: https://github.com/jacobg/f7-phone-input
Demos:

This issue is not related to issue #2. It occurs regardless of that issue fixed or not.

Here is forum post discussing this issue more at length:
http://forum.framework7.io/t/difference-in-behavior-between-inline-templates-and-sfc-templates/5884

Country Code List Button Define their type As "Button"

If I am using this input inside a form and going to select a country code then form submit event call because you did not define the type of button (Where to presenting the country code list). I think it is small issue but we can fixed it easily to add the type "button"

_t

Unknown custom element: <vuephonenumberinput>

When I try to use this plugin in laravel 5.8, I get an error saying the element is not know recognize the custom element. Is there an issue with the element or am using it wrongly?

Edit

On editing, the country code does not match
image

Starting with version 0.2.5 using vue-phone-number-input in a component causes an error

I include the VuePhoneNumberInput inside another component:

...
<script>

  import VuePhoneNumberInput from 'vue-phone-number-input'

  export default {
    components: {
      vphone: VuePhoneNumberInput,
...

This causes the next error:

"export 'default' (imported as 'VuePhoneNumberInput') was not found in 'vue-phone-number-input'

It happens with version 0.2.5 and higher up to last release.

Veevalidate does not show the validation error mesage for phone number input when used with BootstrapVue

In my Laravel+Vue.js SPA ( Single Page Application) I am using the phone number input validation package from here, BootstrapVue from here and Veevalidate from here .

I think I need to show only the code inside my component file. The vue-phone-number-input component shows blue border and HTML5 error message tooltip (?) upon invalid data but no error message appears in red as it happens with name field. My code follows in EditProfile.vue:

<ValidationObserver ref="form" v-slot="{ passes }">

    <div id="registration_form">

        <b-form @submit.prevent="passes(onSubmit)" @reset="resetForm">

            <ValidationProvider vid="name" rules="required|min:2" name="name" v-slot="{ valid, errors }">
                <b-form-group
                        label="User Name:"
                        label-for="exampleInput1"

                >
                    <b-form-input
                            type="text"
                            v-model="name"
                            :state="errors[0] ? false : (valid ? true : null)"
                            placeholder="Enter your name"
                    ></b-form-input>

                    <b-form-invalid-feedback id="inputLiveFeedback">{{ errors[0] }}</b-form-invalid-feedback>

                </b-form-group>

            </ValidationProvider>


            <ValidationProvider vid="mobile" rules="required" name="mobile" v-slot="{ valid, errors }">
            <b-form-group
                    label="Mobile:"
                    label-for="exampleInput1"

            >

                <vue-phone-number-input

                        v-model="mobile"
                        default-country-code="BD"
                        required
                        disable-leading-trailing-space
                        :state="errors[0] ? false : (valid ? true : null)"
                        @update="updatePhoneNumber"
                        placeholder="Enter Mobile Number"
                />

               <b-form-invalid-feedback id="inputLiveFeedback">{{ errors[0] }}</b-form-invalid-feedback>

            </b-form-group>
        </ValidationProvider>



            <b-button type="submit" variant="primary">Submit</b-button>
            <b-button type="reset" variant="danger">Reset</b-button>

        </b-form>

    </div><!-- end of id registration_form-->
</ValidationObserver>`

JS part inside EditProfile.vue is:

`import Datepicker from 'vuejs-datepicker';
import { ValidationObserver, ValidationProvider } from "vee-validate";

export default {
name: "EditProfile",
components: {
ValidationObserver,
ValidationProvider,
Datepicker
},
data: () => ({
name: "",
mobile:""
}),
methods: {

   onSubmit() {
        console.log("Form submitted yay!");
              },

    resetForm() {
        this.name = "";
        this.mobile = "";
        requestAnimationFrame(() => {
            this.$refs.form.reset();
        });
    }

}
};`

When the submit button is pressed then validation works for name field nad for vue-phone-number-input it also happens but no error message shows up in red color.

My Vue.js version is 2.6.10 and I used the latest versions of BootstrapVue and Veevalidate (3) as well.

How can I make the error message appear in red color for vue-phone-number-input element ?

Remove inline important styles

Inline styles (border 1px solid $color !important, etc) makes customization impossible with CSS.
It is worth adding the ability to disable focus styles like validation styles (no-validator-state).

Class is-focused stays if number is valid

The class is-focused stays after phone number is validated/valid, which makes things look a little weird in my example (here I have clicked off the component, so it shouldn't be focused):
Screen Shot 2019-11-04 at 11 28 07

I've fixed it locally by using :focus-selector, but is-focused shouldn't really stay after the element is not focused.

Country code is showing 2 times in input field

i have an issue, please help me to figure out the problem.

i have a phone number value stored in DB as "+92 322 3661422", when i fetch the edit phone number form, vue-phone-number-input does not parsing the country code from the v-model and showing country code 2 times.
please see attachment

issue

How Can I get the country Code In Number

My database design has 2 fields for the phone number one for dial code and the other for the national number so i need these 2 values separately anyone can help me how to extract them separately

How to hide the focus border of input fields?

Hi,

Do you have a suggestion how we could hide the border that appears whenever we focus one of the 2 fields (country selector & input field). I have attached a screenshot.
It gets added by an !import inline-style so I can't really set the border to 0 by overriding the css.

I restyled the component a bit for my usecase so it fits better within my vuetify application, but I don't really like the orange border :-)

2019-05-27_173836

Thanks

Payload

How do i access the payload data "isValid" to make sure that the value input must be valid before moving to the next step

[Feature Request] Set input maxlength based on example format

Hi! I don't know if this feature would be good or not.
Since we are able to show validation of format by changing the color of our input's border, maybe we can activate the max length of the input field based on the example format.

This can be an option if other developers wants to limit it or not because maybe some countries might allow different type of phone number formats. I'm not sure.

Thanks :)

How to bind to E.164 number?

First of all, nice component!

This component is a composition of country code and local number. But it seems that the v-model is just the formatted local number. How do I bind the whole thing? Ideally, it would be able to bind to the E.164 phone number, which is the formattedNumber field in the object emitted by onUpdate event.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.