sexta-feira, 15 de maio de 2015

Máscara para CPF e CNPJ no iReport

Clique com o botão direito no text field ou crie uma variável e selecione Edit Expression. 

Variável = Variable Expression

O código abaixo testa o tamanho do campo. Se for igual a 14 dígitos, formata para CNPJ, caso contrário, formata para CPF. 

( $F{CGC_CPF}.length() == 14 ? 
($F{CGC_CPF}.substring(0, 2) + "." + 
$F{CGC_CPF}.substring(2, 5) + "." + 
$F{CGC_CPF}.substring(5, 8) + "/" + 
$F{CGC_CPF}.substring(8, 12) + "-" + 
$F{CGC_CPF}.substring(12, 14)) : 
($F{CGC_CPF}.substring(0, 3) + "." + 
$F{CGC_CPF}.substring(3, 6) + "." + 
$F{CGC_CPF}.substring(6, 9) + "-" + 
$F{CPFCNPJ}.substring(9, 11)) )  

2 comentários: