How to receive the CPF number and format to do without the points and dash (input Mask)

I am creating a mask with inputMask, but the CPF is received like this: 222.222.488-19.

And I want it to have the mask of No input but at the time of sending it will be without the dots and the dash. Like this 22222248819. My code:

@Component({
  selector: 'app',
  template: `
    <input [textMask]="{mask: mask}" [(ngModel)]="myModel" type="text"/>
  `
})
export class AppComponent {
  public myModel = ''
  public mask = ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
}
Author: Gato de Schrödinger, 2018-11-22

7 answers

    var variavel=$("#campo").val();//atribui o valor a variavel
    var variavel2 = variavel.replace('.', ''); //remove UM ponto
  var variavel2 = variave2.replace(',', ''); //remove UMA virgula
  var variavel2 = variave2.replace('-', ''); //remove UM traço

BELOW VAII EXPLAINED

$(document).ready(function(){
$('.cpf').mask('000.000.000-00', {reverse: true});
  $('.cnpj').mask('00.000.000/0000-00', {reverse: true});
  $('.dinheiro').mask('000.000,00', {reverse: true});
 
 
$("#Botao").on("click", function(e){
e.preventDefault(e);

//Variavel com valor do campo dinheiro
var dinheiro=$("#dinheiro").val();
//retira o ponto coloca o restante em dinheiro 2.
var dinheiro2 = dinheiro.replace('.', '');
//retirar a virgula, e colocar o restante na dinheiro2.
var dinheiro2 = dinheiro2.replace(',', '');
//RECUPERAR VALORES do dinheiro.
alert(" O DINHEIRO COM MASCARA FOI : " +dinheiro+ " O DINHEIRO SEM MASCARA È: "+dinheiro2);
//Variavel com valor do campo cnpj
var cnpj=$("#cnpj").val();
var cnpj2 = cnpj.replace('.', '');
var cnpj2 = cnpj2.replace('.', '');
//*se tiver 1 ponto, dá um replace no ponto. se tiver 10... dá 10 replace.
var cnpj2 = cnpj2.replace('-', '');
var cnpj2 = cnpj2.replace('/', '');
//RECUPERAR VALORES do dinheiro.
alert(" O CNPJ COM MASCARA FOI : " +cnpj+ " O CNPJ SEM MASCARA È: "+cnpj2);

//Variavel com valor do campo CPF
var cpf=$("#cpf").val();
var cpf2 = cpf.replace('.', '');
var cpf2 = cpf2.replace('.', '');
//*se tiver 1 ponto, dá um replace no ponto. se tiver 10... dá 10 replace.
var cnpj2 = cnpj2.replace('-', '');
//RECUPERAR VALORES do dinheiro.
alert(" O CPF COM MASCARA FOI : " +cpf+ " O DO CPF SEM MASCARA È: "+cpf2);

})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
<!-- ENTENDA OS CAMPOS PELO ID. é o id que vaos usar para recuperar os valores.-->
    <input class="dinheiro" id="dinheiro" placeholder="DIGITE UM VALOR EM DINHEIRO">
    <input class="cnpj" id="cnpj" placeholder="DIGITE UM CNPJ">
    <input class="cpf" id="cpf" placeholder="DIGITE UM CPF">
   <br>
     <br>
       <br>
 PREENCHA TODOS OS CAMPOS E <br>
<button type="" id="Botao">CLIQUE AKI</button>
 <!-- PULE PARA O EVENTO JAVASCRIPT-->
 3
Author: Risk, 2018-11-22 02:00:25

Just to supplement the previous answers. If you want to take both at once (DoT and dash). My answer is with pure JS.

let botao = document.querySelector("#botao");

botao.onclick = function() {
  let cpf_com_ponto_e_traco = document.querySelector("#cpf-com-ponto-e-traco").value;
  let cpf_sem_ponto_e_traco = cpf_com_ponto_e_traco.replace(".", "").replace(".", "").replace("-", "");

  console.log(cpf_sem_ponto_e_traco);
}
<input type="text" id="cpf-com-ponto-e-traco" value="111.222.333-44" readonly>
<button id="botao">Pegar CPF</button>

And another alternative to not having to use replace twice to take the first two dots and another replace to take the dash. For this we use a regular expression in the first parameter of replace that only allows alphanumeric characters in that string. Whatever is not a number, the replace exchanges by the second parameter. See:

let botao = document.querySelector("#botao");

botao.onclick = function() {
  let cpf_com_ponto_e_traco = document.querySelector("#cpf-com-ponto-e-traco").value;
  let cpf_sem_ponto_e_traco = cpf_com_ponto_e_traco.replace(/[^0-9]/g, "");

  console.log(cpf_sem_ponto_e_traco);
}
<input type="text" id="cpf-com-ponto-e-traco" value="111.222.333-44" readonly>
<button id="botao">Pegar CPF</button>
 2
Author: Gato de Schrödinger, 2020-06-09 16:58:33

Creates a function that removes these characters using regexp ali in mask. For example:

function somenteNumeros(cpf){
  let numeros = cpf.toString().replace(/\.|-/gm,'');
  if(numeros.length === 11)
   return numeros;

  return 'cpf inválido'
 }

 export class AppComponent {
   public myModel = ''
   public mask = somenteNumeros(cpf)
 }

If you need to do several regex tests, I recommend the site: https://regex101.com /

 1
Author: Cmte Cardeal, 2018-11-22 01:00:28

Follows a solution with regex, where you do not have to make several replaces, it will capture only the digits in a array and convert them into a string with the function join.

Example:

let botao = document.querySelector("#botao");

botao.onclick = function() {

  let valorOriginal = document.querySelector("#cpf").value;
  let regex = /\d/g;
  let valorFiltrado = valorOriginal.match(regex);
  
  console.log(valorFiltrado.join(""));
}
<input type="text" id="cpf" value="455.666.727-99" readonly>
<button id="botao">Submit</button>

For those who love one-line solutions:

console.log("455.546.727-47".match(/\d/g).join(""));

Explanation

  • we get the value of cpf field with query selector and assign in variable valorOriginal
  • then we declare the regex variable with the value /\d/g;
    The first / shows javascript that we are declaring a variable of type regular Expression
    The \d next represents what we want to capture, a digit.
    We end the regex statement with /
    And finally we put the g, signaling that this regex should go through the entire string.
  • next we will use the function match to return what the regex captures as an Array.
  • and to display the value as a string we use the function join(""), it will return all our values in a string without comma separation, as it would normally do if we used only the function toString().
 1
Author: Paz, 2020-06-03 18:41:04
var cpf = '000.000.000-00';
var string = cpf.replace('.','').replace('-','').replace('.','') ;
 1
Author: João Vitor Fontes, 2020-06-03 20:02:04

You can do directly in your form before submitting it through replace ():

formulario: FormGroup; this.formulario.value.cpf = this.formulario.value.cpf.replace(/\.|-/gm,'');

Obs.: whereas your formControlName="cpf".

 0
Author: Fábio Pereira, 2019-09-09 15:47:54

In JS has the function replace().
You can do something like cpfComMascara.replace("\D", "")
In regex \d means digit. The capital letter is the opposite.
Soon, regex will replace the non-digit with empty.

 0
Author: Mario Bressan, 2020-06-03 18:09:38