Why can't I validate input on a masked variable type?

Casey23
Tera Guru

I am having an issue where I can't validate data in a masked field and am wondering if anyone knows why, and/or has a solution?

 

What I'm trying to do is validate a TIN by using an onChange client script. I've validated the script works if the field is a single line text, but when I try it on a masked variable, it immediately removes any input and shows the field message.

 

Has anyone accomplished this?

 

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

   //Allow only numbers or XX-XXXXXXX on the TIN field   
	var regexp = /^\d{2}-?\d{7}$/;
    if (!regexp.test(newValue)) {        
        g_form.setValue('tin_masked', '');
        var myElement = g_form.getControl('tin_masked');
        g_form.flash("label_" + myElement.id, "#FFFACD", 0);
        g_form.showFieldMsg('tin_masked', "Please enter TIN in the format ##-#######", 'error');
		return false;
    }
}

 

 

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

Hi @Casey23 

 

The Masked field in ServiceNow is intended to provide a visual representation of data rather than a direct input mechanism.

That is why the input entered into a Masked field is not a direct string value, and the onChange event may behave differently compared to a regular text field.

However, you can work around this issue by using a Text field to capture the input, and then using a Masked field to display it in the desired format. Here's how you can achieve this:

  1. Create a Text field (e.g., tin_input) to capture the user's input in the expected format (##-#######).

  2. Create a Masked field (e.g., tin_masked) to display the masked value in the ##-####### format.

  3. Create an onChange client script for the tin_input field:

 

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

    var regexp = /^\d{2}-?\d{7}$/;
    if (!regexp.test(newValue)) {        
        g_form.setValue('tin_input', '');
        g_form.clearMessages('tin_input');
        g_form.addErrorMessage('Please enter TIN in the format ##-#######');
        return false;
    }

    var formattedValue = newValue.replace(/(\d{2})(\d{7})/, '$1-$2');
    g_form.setValue('tin_masked', formattedValue);
}

 

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

View solution in original post

2 REPLIES 2

Tushar
Kilo Sage
Kilo Sage

Hi @Casey23 

 

The Masked field in ServiceNow is intended to provide a visual representation of data rather than a direct input mechanism.

That is why the input entered into a Masked field is not a direct string value, and the onChange event may behave differently compared to a regular text field.

However, you can work around this issue by using a Text field to capture the input, and then using a Masked field to display it in the desired format. Here's how you can achieve this:

  1. Create a Text field (e.g., tin_input) to capture the user's input in the expected format (##-#######).

  2. Create a Masked field (e.g., tin_masked) to display the masked value in the ##-####### format.

  3. Create an onChange client script for the tin_input field:

 

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

    var regexp = /^\d{2}-?\d{7}$/;
    if (!regexp.test(newValue)) {        
        g_form.setValue('tin_input', '');
        g_form.clearMessages('tin_input');
        g_form.addErrorMessage('Please enter TIN in the format ##-#######');
        return false;
    }

    var formattedValue = newValue.replace(/(\d{2})(\d{7})/, '$1-$2');
    g_form.setValue('tin_masked', formattedValue);
}

 

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

I'm not sure this is accurate. If you're just typing into a text field and not masking as you're typing you're not solving for the use case, which could be someone using a computer at a library, or in a public space where you don't want plain text values on the screen.

It seems an onsubmit validation would be the better way to go, as much as I dislike it.