Skip to content

Commit

Permalink
add validations and emit number event
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundersoul committed Sep 10, 2017
1 parent 100622c commit 0b7d409
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ npm install ng2-tel-input
After installing, you need to add "intlTelInput.css" in your index.html.
Once done, we are ready to use this library.

In order to use this directive, you need to add "[ng2TelInput]" with options to your text field. For example,"<input type="text" [ng2TelInput]="{setCountry: 'gb'}" />"
# How to use

In order to use this directive, you need to add "ng2TelInput" directive with "[ng2TelInputOptions]" options to your text field. For example,
<input type="text"
ng2TelInput
[ng2TelInputOptions]="{initialCountry: 'in'}"
(hasError)="hasError($event)"
(ng2TelOutput)="getNumber($event)"
(countryChange)="onCountryChange($event)" />

![N|Solid](https://github.com/gauravsoni119/ng2-tel-input/blob/master/example.png)
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ng2-tel-input",
"version": "1.0.8",
"description": "An example repository for building your own Angular 2 library",
"version": "1.0.9",
"description": "An Angular 2 wrapper for intl-tel-input library",
"main": "ng2-intl-tel-input.js",
"scripts": {
"watch": "tsc -p src -w",
Expand All @@ -13,17 +13,16 @@
"url": "git+https://github.com/gauravsoni119/ng2-tel-input.git"
},
"keywords": [
"Angular",
"2",
"Angular2",
"Library",
"Seed"
"intl-tel-input"
],
"author": "jhades.dev@gmail.com",
"author": "sonigaurav119@gmail.com",
"license": "MIT",
"bugs": {
"url": "https://github.com/jhades/angular2-library-seed/issues"
"url": "git+https://github.com/gauravsoni119/ng2-tel-input.git"
},
"homepage": "https://github.com/jhades/angular2-library-seed#readme",
"homepage": "https://github.com/gauravsoni119/ng2-tel-input#readme",
"devDependencies": {
"@types/intl-tel-input": "0.0.8",
"@types/jquery": "<2.2",
Expand All @@ -38,7 +37,7 @@
"@angular/platform-browser": "^2.4.10",
"@angular/platform-browser-dynamic": "^2.4.10",
"es6-shim": "^0.35.3",
"intl-tel-input": "^9.2.0",
"intl-tel-input": "^12.0.3",
"jquery": "^3.1.1",
"reflect-metadata": "^0.1.10",
"systemjs": "^0.19.39",
Expand Down
30 changes: 22 additions & 8 deletions src/ng2-intl-tel-input.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
import { Directive, ElementRef, EventEmitter,
HostListener, Input, OnInit, Output } from '@angular/core';
import * as $ from 'jquery';
import 'intl-tel-input';
import 'intl-tel-input/build/js/utils';
Expand All @@ -7,20 +8,33 @@ import 'intl-tel-input/build/js/utils';
selector: '[ng2TelInput]',
})
export class Ng2TelInput implements OnInit {
@Input('ng2TelInput') ng2TelInput: any;
@Input('ng2TelInputOptions') ng2TelInputOptions: any;
@Output('hasError') hasError: EventEmitter<boolean> = new EventEmitter();
@Output('ng2TelOutput') ng2TelOutput: EventEmitter<any> = new EventEmitter();
@Output('countryChange') countryChange: EventEmitter<any> = new EventEmitter();
ngTelInput: any;
constructor (private el: ElementRef) {}
ngOnInit() {
this.ngTelInput = $(this.el.nativeElement);
console.log(this.ng2TelInput);
this.ngTelInput.intlTelInput(this.ng2TelInput);
this.ngTelInput.intlTelInput(this.ng2TelInputOptions);
this.ngTelInput.on("countrychange", (e: any, countryData:any) => {
this.countryChange.emit(countryData);
});
}

@HostListener('blur') onBlur() {
if (this.ngTelInput.intlTelInput('isValidNumber')) {
console.log('valid number');
} else {
console.log('invalid number');
let isInputValid:boolean = this.isInputValid();
if (isInputValid) {
let telOutput = this.ngTelInput.intlTelInput("getNumber");
this.hasError.emit(isInputValid);
this.ng2TelOutput.emit(telOutput);
} else
{
this.hasError.emit(isInputValid);
}
}

isInputValid(): boolean {
return this.ngTelInput.intlTelInput('isValidNumber') ? true : false;
}
}

0 comments on commit 0b7d409

Please sign in to comment.