How to Auto Format a Single Line Text field of a record producer?

Abhi33
Tera Expert

Hi,

 

How to autoformat a single line text(number) field on a record producer.

Only 10 numbers can be entered and it auto-formats to 123-456-7890.

I used regex by creating a new rule using this ^\d{3}-\d{3}-\d{4}$, but I would like to know how can we auto fromat it.

 

 

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hi @Abhi33 

You can create onChange Catalog Client script on the variable. Please check once with script shown below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldValue = g_form.getValue("your_variable_name_here");
    fieldValue = fieldValue.replaceAll("-", "");
    if (fieldValue.length == 10 && !isNaN(fieldValue)) {
        var newFieldValue = fieldValue.substring(0,4) + "-" + fieldValue.substring(4,7) + "-" + fieldValue.substring(7);
        g_form.setValue("your_variable_name_here", newFieldValue);
    } else {
        g_form.clearValue("your_variable_name_here");
        g_form.addErrorMessage("Please enter valid value having 10 Numeric character or value in 012-345-6789 format");
    }
}

 

View solution in original post

2 REPLIES 2

Sonu Parab
Mega Sage
Mega Sage

Hi @Abhi33 , To auto format, create one onChange Catalog client script on that record producer and add below script.

 

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

    //Type appropriate comment here, and begin script below
    var pattern = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/; //xxx xxx-xxxx   
    var phone = g_form.getValue('number'); // Here 'number' will be the variable name
    if (!pattern.test(phone)) {
        phone = phone.replace(/\D/g, '');
        var regex = /^\d{10}$/;
        var is_valid = regex.test(phone);
        if (!is_valid) {
            g_form.clearValue('number');
            g_form.showFieldMsg("number", "Must have exactly 10 numeric characters.", "error");
        } else {
            phone = phone.slice(0, 3) + '-' + phone.slice(3, 6) + '-' + phone.slice(6, 10);
            g_form.setValue('number', phone);
        }
    }

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank you

Mahendra RC
Mega Sage

Hi @Abhi33 

You can create onChange Catalog Client script on the variable. Please check once with script shown below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldValue = g_form.getValue("your_variable_name_here");
    fieldValue = fieldValue.replaceAll("-", "");
    if (fieldValue.length == 10 && !isNaN(fieldValue)) {
        var newFieldValue = fieldValue.substring(0,4) + "-" + fieldValue.substring(4,7) + "-" + fieldValue.substring(7);
        g_form.setValue("your_variable_name_here", newFieldValue);
    } else {
        g_form.clearValue("your_variable_name_here");
        g_form.addErrorMessage("Please enter valid value having 10 Numeric character or value in 012-345-6789 format");
    }
}