I need to format a mask for CNPJ. Until then it is done, but the company default is to format with space instead of point.
This is the code I am using.
valorDoTextBox = valorDoTextBox.replace(/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3")
Someone can explain to me how this part (/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3")
works so you can take the stitches and put a space.
Thank you for being able to help me.
function MascaraParaLabel(valorDoTextBox) {
if (valorDoTextBox.length <= 14) {
//Coloca ponto entre o segundo e o terceiro dígitos
valorDoTextBox = valorDoTextBox.replace(/^(\d{2})(\d)/, "$1.$2")
//Coloca ponto entre o quinto e o sexto dígitos
valorDoTextBox = valorDoTextBox.replace(/^(\d{2})\.(\d{3})(\d)/, "$1 $2 $3")
//Coloca uma barra entre o oitavo e o nono dígitos
valorDoTextBox = valorDoTextBox.replace(/\.(\d{3})(\d)/, ".$1/$2")
//Coloca um hífen depois do bloco de quatro dígitos
valorDoTextBox = valorDoTextBox.replace(/(\d{4})(\d)/, "$1-$2")
}
return valorDoTextBox
I pass the value of the unformatted textbox 14397462000109 and the label at the front shows 14 397 462 / 0001-09 formatted in the company standard.