Can I validate a string with only alphanumeric, space and underscore chars only

etabalon
Mega Expert

Good day!

Can I validate a string and only accepts upper/lower case alphanumeric, space, dash and underscore chars only?

I'm trying to validate a server name so users cannot use like $ or % characters as part of the server name.

Thanks,

Enrique

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

Here's a regex that should work, including numeric characters:



if (/^[-\w ]+$/.test(server_name)) {   }   // server_name is OK


else { }   // server_name NOT OK



\w includes [a-zA-Z0-9_]



I suggest running this in both client and server.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile


visit snowaid


View solution in original post

11 REPLIES 11

John, thank you so much for the details. I appreciate it.


Abhinay Erra
Giga Sage

This is a regular expression syntax which can be used in servicenow as well. Servicenow supports all the Javascript methods. Reference to regular expressions


RegExp \w Metacharacter