Created
October 22, 2017 00:59
-
-
Save TomCosta/9c19b9bd6987e3349e0da0491dc30940 to your computer and use it in GitHub Desktop.
Ionic 3 CPF/CNPJ input mask. Independent of which is the inputted data either a CPF or a CNPJ. It add mask.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form #loginForm="ngForm"> | |
<ion-item> | |
<ion-label floating>CPF/CNPJ</ion-label> | |
<ion-input [(ngModel)]="cpf_cnpj" (blur)="cpf_cnpj = format(cpf_cnpj)" name="cpf_cnpj"></ion-input> | |
</ion-item> | |
<button ion-button full type="submit" color="sicor" (tap)="login(signForm.value)">Login</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MenuController, NavParams, ModalController } from 'ionic-angular'; | |
import { IonicPage, NavController } from 'ionic-angular'; | |
import { AlertController } from 'ionic-angular'; | |
import { Component } from '@angular/core'; | |
@IonicPage() | |
@Component({ | |
selector: 'page-login', | |
templateUrl: 'login.html', | |
}) | |
export class LoginPage { | |
cpf_cnpj = ''; | |
DECIMAL_SEPARATOR="."; | |
GROUP_SEPARATOR=","; | |
pureResult: any; | |
maskedId: any; | |
val: any; | |
v: any; | |
constructor( | |
public modalCtrl: ModalController, | |
private alertCtrl: AlertController, | |
private menu: MenuController, | |
public navCtrl: NavController, | |
){} | |
ionViewDidEnter() { | |
this.menu.swipeEnable(false); | |
} | |
ionViewWillLeave(){ | |
this.menu.swipeEnable(true); | |
} | |
format(valString) { | |
if (!valString) { | |
return ''; | |
} | |
let val = valString.toString(); | |
const parts = this.unFormat(val).split(this.DECIMAL_SEPARATOR); | |
this.pureResult = parts; | |
if(parts[0].length <= 11){ | |
this.maskedId = this.cpf_mask(parts[0]); | |
return this.maskedId; | |
}else{ | |
this.maskedId = this.cnpj(parts[0]); | |
return this.maskedId; | |
} | |
}; | |
unFormat(val) { | |
if (!val) { | |
return ''; | |
} | |
val = val.replace(/\D/g, ''); | |
if (this.GROUP_SEPARATOR === ',') { | |
return val.replace(/,/g, ''); | |
} else { | |
return val.replace(/\./g, ''); | |
} | |
}; | |
cpf_mask(v) { | |
v = v.replace(/\D/g, ''); //Remove tudo o que não é dígito | |
v = v.replace(/(\d{3})(\d)/, '$1.$2'); //Coloca um ponto entre o terceiro e o quarto dígitos | |
v = v.replace(/(\d{3})(\d)/, '$1.$2'); //Coloca um ponto entre o terceiro e o quarto dígitos | |
//de novo (para o segundo bloco de números) | |
v = v.replace(/(\d{3})(\d{1,2})$/, '$1-$2'); //Coloca um hífen entre o terceiro e o quarto dígitos | |
return v; | |
} | |
cnpj(v) { | |
v = v.replace(/\D/g, ''); //Remove tudo o que não é dígito | |
v = v.replace(/^(\d{2})(\d)/, '$1.$2'); //Coloca ponto entre o segundo e o terceiro dígitos | |
v = v.replace(/^(\d{2})\.(\d{3})(\d)/, '$1.$2.$3'); //Coloca ponto entre o quinto e o sexto dígitos | |
v = v.replace(/\.(\d{3})(\d)/, '.$1/$2'); //Coloca uma barra entre o oitavo e o nono dígitos | |
v = v.replace(/(\d{4})(\d)/, '$1-$2'); //Coloca um hífen depois do bloco de quatro dígitos | |
return v; | |
} | |
public login(formData) { | |
....you auth code here. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment