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

Client script for checking special characters in a field

AmritaT
Tera Expert

I have a requirement to validate a field which should not have the following characters ' @ , . / \ ' .

I am using the following script :

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

var specialCharRegex = /[^/@,.\\]/;
if(specialCharRegex.test(newValue))
{
alert('Please re-enter a valid database name. Special characters such as [@ . /] are not allowed.');
}
g_form.setValue('database_name', '');
}

.......

This script is not validating as per the requirement. Please advice.

 

1 ACCEPTED SOLUTION

AmritaT
Tera Expert

After few trials and errors, used the following code which worked finally 🙂

if( newValue.indexOf('@')> -1 || newValue.indexOf(',')> -1 || newValue.indexOf('/')> -1 || newValue.indexOf('\\')> -1 || newValue.indexOf('.')>-1){
alert('Please re-enter a valid database name. Special characters such as \\ , @ . / are not allowed.');
g_form.setValue('database_name', '');
}

View solution in original post

2 REPLIES 2

Siddiq2
Tera Contributor

Hi Amrita,

Please try below link.

https://community.servicenow.com/community?id=community_question&sys_id=1906a3f4dbfdb3485129a851ca961997

Thanks,

Siddiq

AmritaT
Tera Expert

After few trials and errors, used the following code which worked finally 🙂

if( newValue.indexOf('@')> -1 || newValue.indexOf(',')> -1 || newValue.indexOf('/')> -1 || newValue.indexOf('\\')> -1 || newValue.indexOf('.')>-1){
alert('Please re-enter a valid database name. Special characters such as \\ , @ . / are not allowed.');
g_form.setValue('database_name', '');
}