How to create a mask to format a field in the workspace?

Moacir Junior
Tera Contributor

I need to insert formatting in a field on the workspace form.
The field must follow the format 00.000.000/0000-00.
I managed to make it work in the Catalog Item, but it doesn't work in the workspace.

MoacirJunior_0-1698778370562.png

MoacirJunior_1-1698778519164.png

How can I insert this formatting in the workspace?

 

1 ACCEPTED SOLUTION

Hi @Moacir Junior 

I see. Thanks for the recording.

getControl will not works in Workspace. And you will also face the same problem on the Portal.

You can find the error in the Browser Console.

Also Ref: KB0855260

 

Cheers,

Tai Vu

View solution in original post

3 REPLIES 3

Tai Vu
Kilo Patron
Kilo Patron

Hi @Moacir Junior 

Let's try this approach.

1. Define a Variable Validation Regex.

Sample below

TaiVu_0-1698822460861.png

^[0-9]{2}(?:.[0-9]{3})(?:.[0-9]{3})\/[0-9]{4}(?:-[0-9]{2})?$

 

2. In your Producer Variable at section Type Specifications, select the Validation Regex as CPF (from step 1).

 

Enjoy the result.

TaiVu_1-1698822562845.png

 

Let me know if it works for you.

 

Cheers,

Tai Vu

 

Moacir Junior
Tera Contributor

Hi @Tai Vu, thank you for your response.

The Validate Regex application works well, but what I require is for the formatting to be applied automatically as the user types, just like the example I demonstrated in this video.

 

To achieve this functionality, I've created three Catalog Client Scripts. Two of them are designed to validate fields, while the third one is intended to format them, as shown in the video. Here are the scripts I've created:

 

CNPJ field validation
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

       var regEx = /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/;
   
    if (regEx.test(newValue)) {
        g_form.clearMessages('variables.u_cnpj');
     
    } else {
        g_form.clearValue('variables.u_cnpj');
        g_form.clearMessages('variables.u_cnpj');
        g_form.showFieldMsg('variables.u_cnpj', 'Formato CNPJ inválido. Digite numero corretamente' ,  'error');
    }
}
CPF Field Validation
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    // Validação de CPF
    var regEx = /^(\d{3}\.){2}\d{3}-\d{2}$/;


    if (regEx.test(newValue)) {
        g_form.clearMessages('u_cpf'); // Limpa mensagens de erro existentes
        // g_form.showFieldMsg('u_cpf', 'Este CPF é Válido', 'info');
    } else {
        g_form.clearValue('u_cpf'); // Limpa o valor do campo CPF
        g_form.clearMessages('u_cpf'); // Limpa mensagens de erro existentes
        g_form.showFieldMsg('u_cpf', 'Formato CPF inválido. Digite numero corretamente', 'error');
    }
}
 
Formats CNPJ and CPF fields
function onLoad() {
    var cpfField = g_form.getControl('u_cpf');
    cpfField.maxLength = 14;

    cpfField.addEventListener('input', function (event) {
        var cpf = event.target.value;
        cpf = cpf.replace(/\D/g, '');
        cpf = cpf.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4');
        event.target.value = cpf;
    });

    var cnpjField = g_form.getControl('u_cnpj');
    cnpjField.maxLength = 18;

    cnpjField.addEventListener('input', function (event) {
        var cnpj = event.target.value;
        cnpj = cnpj.replace(/\D/g, '');
        cnpj = cnpj.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
        event.target.value = cnpj;
    });
}
 

The field validation scripts are functioning correctly, but the formatting script (the last one mentioned above) is not working as expected.

I would appreciate your guidance on how to apply this formatting to the workspace.

Hi @Moacir Junior 

I see. Thanks for the recording.

getControl will not works in Workspace. And you will also face the same problem on the Portal.

You can find the error in the Browser Console.

Also Ref: KB0855260

 

Cheers,

Tai Vu