The CreatorCon Call for Content is officially open! Get started here.

Not allow special characters in a catalog variable

Dave_p
Giga Guru

Hi,

I have a requirement where i should not be able to enter some special characters. I tried it but it is not working. Kindly help. Please don't post links.

 

1.png

 

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

    var pattern = /^[~#%&*{}:<>?/|"\–]*$/;
	if(!pattern.test(newValue)){
		g_form.addErrorMessage('The following characters cannot be entered' + ' ~, #, %, & , *, {, }, \, :, <, >, ?, /, |, ", –');
		g_form.clearValue('outlook_distribution_group_list_name');
	}


}

 

2.png

 

Regards

Suman P.

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Dave_p 

try this and it will only allow a to z, A to Z and numbers

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

    g_form.hideFieldMsg('outlook_distribution_group_list_name');

    // Regular expression to allow only alphabets and numbers
    var regex = /^[a-zA-Z0-9]+$/;

    if (!regex.test(newValue)) {
        // Display an error message if the input is invalid
        g_form.showFieldMsg('outlook_distribution_group_list_name', 'Only alphabets and numbers are allowed.', 'error');
        // Clear the invalid input
        g_form.clearValue('outlook_distribution_group_list_name');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Dave_p
Giga Guru

Hi @J Siva , @Ankur Bawiskar , @GopikaP 

 

I simply used this and it works.

 

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

    var pattern = /^[a-zA-Z0-9-.,]*$/;
	if(!pattern.test(newValue)){
		g_form.addErrorMessage('Special characters cannot be entered');
		g_form.setValue('outlook_distribution_group_list_name', '');
	}


}

 

Regards

Suman P.

View solution in original post

6 REPLIES 6

GopikaP
Mega Sage

Hi @Dave_p , Can you try this one? 

var pattern = /^[^~#%&*{}\\:<>\?/|\"–]*$/;
   if(!pattern.test(newValue)){
		g_form.addErrorMessage('The following characters cannot be entered' + ' ~, #, %, & , *, {, }, \, :, <, >, ?, /, |, ", –');
		g_form.clearValue('outlook_distribution_group_list_name');
	}

J Siva
Kilo Patron
Kilo Patron

Hi @Dave_p 

You can achieve this via variable regex.

Regex: ^[a-zA-Z0-9\s]+$

Step1: Create variable validation regex

Step2: Select that under "Type specifications" section validation regex field.

 

Screenshot_20250318-195014~2.pngScreenshot_20250318-195406~2.pngScreenshot_20250318-195519~2.pngScreenshot_20250318-195547~2.png

Hope this helps.

Regards,

Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@Dave_p 

try this and it will only allow a to z, A to Z and numbers

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

    g_form.hideFieldMsg('outlook_distribution_group_list_name');

    // Regular expression to allow only alphabets and numbers
    var regex = /^[a-zA-Z0-9]+$/;

    if (!regex.test(newValue)) {
        // Display an error message if the input is invalid
        g_form.showFieldMsg('outlook_distribution_group_list_name', 'Only alphabets and numbers are allowed.', 'error');
        // Clear the invalid input
        g_form.clearValue('outlook_distribution_group_list_name');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Dave_p 

Seems you marked your own response as correct.

Remember marking your own response as correct doesn't add up to the correct answer count for user profile.

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader