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

@Dave_p 

Would you mind marking my response as correct if that worked for you?

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

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.