How to mask variables first digits and just show last four

BijoyDeb
Tera Contributor

Hi all,

 

I have created 2 plain text variables on my record producer. When users enters a number in the 1st field, I want to mask its initial digits and just show last 4 digits in 2nd variable.

 

For eg. If user enters 123456789 in 1st variable

Then 2nd variable should get auto populated with #####6789 this value.

 

How can I achieve this?

 

4 REPLIES 4

Mark Manders
Mega Patron

Create an onChange client script (on change of your first variable):

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

    // The first variable name
    var firstVariable = 'u_first_variable';
    // The second variable name
    var secondVariable = 'u_second_variable';

    // Retrieve the value from the first variable
    var firstValue = g_form.getValue(firstVariable);
    
    // Ensure the first value is not empty and has at least 4 digits
    if (firstValue.length >= 4) {
        // Mask all digits except the last 4
        var maskedValue = '#'.repeat(firstValue.length - 4) + firstValue.slice(-4);
        
        // Set the masked value to the second variable
        g_form.setValue(secondVariable, maskedValue);
    } else {
        // If the first value has less than 4 digits, simply set it as it is
        g_form.setValue(secondVariable, firstValue);
    }
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Rohits6500
Tera Contributor

Hi @BijoyDeb,

You can achieve this requirement by writing on change client script.

Find attach code.

 

var inputVariable = "123456789"; // get value using g_form.getValue("field_name");
var maskedValue = "#####" + inputVariable.substring(inputVariable.length - 4);  
var secondVariable = maskedValue;
//set value using g_form.setValue("field_name",secondVariable);
 
 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi

Satishkumar B
Giga Sage
Giga Sage

Hi @BijoyDeb 
Try this:

 

 

// Function to handle the change event of the first variable
function onChange(control, oldValue, newValue) {
    // Get the value from the first variable
    var firstValue = g_form.getValue('first_variable');
    
    // Check if the value is not empty and has more than 4 digits
    if (firstValue && firstValue.length > 4) {
        // Mask all but the last 4 digits
        var maskedValue = '#'.repeat(firstValue.length - 4) + firstValue.slice(-4);
        // Set the value to the second variable
        g_form.setValue('second_variable', maskedValue);
    } else {
        // If value is less than or equal to 4 digits, just display it as is
        g_form.setValue('second_variable', firstValue);
    }
}

// Bind the function to the 'onChange' event of the first variable
g_form.onChange('first_variable', onChange);

 

 

……………………………………………………………………………………………………

Please Mark it helpful 👍'and Accept Solution!✔️! If this helps you!!

 

Sandeep Rajput
Tera Patron
Tera Patron

@BijoyDeb You can create an onChange script on the first variable as follows.

 

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

    var firstVar = newValue;
    var maskedString = '';
    if (firstVar.length > 4) {        
        for (i = 0; i < firstVar.length; i++) {
            if (firstVar.length - i > 4) {
                maskedString = maskedString + '#';
            } else {
                maskedString = maskedString + firstVar[i];
            }
        }
    }
	g_form.setValue('second_variable', maskedString); //set the value of second variable here
}