Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more 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

@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  ||  10x 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

7 REPLIES 7

@Dave_p 

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

Regards,
Ankur
Certified Technical Architect  ||  10x 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.

D V Sandeep
Tera Contributor

 @Ankur Bawiskar @J Siva 
Can we restrict special characters (% $ @ ( )  &  <   >  ' ") in the attachment type variable?