I’m trying to validate a string field to check for certain characters and raise an error if any of them are present. This is the regular expression I’m using:
var regex = /^[^'\\\/\:\*\?"<>\|-]*$/; // In ServiceNow
The JavaScript code in ServiceNow
platform is as follows:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regex = /^[^'\\\/\:\*\?"<>\|-]*$/;
var ans = g_form.getValue('u_native_renaming_suffix');
if(!regex.test(ans)){
alert('Please enter valid string');
return;
}
}
It is working perfectly for all the included characters. However, when elongated hyphen (–
) or a curvy single or double quote (“
, ”
, ‘
, ’
) (which we find in apps like MS Word) are used, instead of the normal hyphen or quotes, this validation doesn’t work. That is, it is raising an error when normal hyphen or quotes are used, but not when elongated hyphen or curvy quotes are used.
I even tried copy-pasting the new characters into the regular expression, but they are just being replaced or treated as the normal ones, i.e.:
/^[^'’\\\/\:*\?"”<>\|-–]*$/
turns into
/^[^''\\\/\:*\?""<>\|-–]*$/
when I hit save.
How do I incorporate the validation for those 3 characters as well?