How to mask variables first digits and just show last four
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 02:22 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:24 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:33 AM
Hi @BijoyDeb,
You can achieve this requirement by writing on change client script.
Find attach code.
Please mark my response as correct and helpful if it helped solved your question.
Thanks,
Rohit Suryawanshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 03:43 AM
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!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 04:08 AM
@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
}