Removing special characters from a string field

natty29
Kilo Contributor

Is there a way to prevent/remove special characters from displaying in the Short Description (string) field on a form?

For example, users are able to copy and paste a title of a knowledge article, including the star rating. This is then carried through to subsequent forms: Sample - Example article (★★★★★). I would like to have these characters removed upon save or with an alert to prompting the user to "Please remove special characters".

1 REPLY 1

Brian Dailey1
Kilo Sage

Hi natty,

 

I would suggest using a string Match method along with some form of RegEx to identify anything that isn't what you want.  e.g., have it match on anything that isn't alphanumeric.

 

For example, try this just to see how it works:

var str = "1234%&$1230kjsfd.,><?";
var test = str.replace(/[^\d\w]/gi, '');
alert("Str: " + str + "\ntest: " + test);

 

Here is my favorite quickref on using RegEx from DZone RefCardz.  Let us know if that works for you.

 

Thanks,

-Brian