Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need Regular Expression for 30 character limit with no spaces or special characters

chrish5
Giga Guru

Hi Community,

I am currently using a regular expression of .{1,30} to limit the characters to 30 on a field.  I want to add to it so no spaces (beginning, middle or end) are allowed and no special characters are allowed.  Can anybody help with this?   Thanks!

1 ACCEPTED SOLUTION

Hi @chrish5,

 

This should be better 🙂 

 

^([A-z]|[0-9]){1,30}$


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

13 REPLIES 13

Yup, works correctly trying it from the Catalog Item, but not the Service Portal.  Thanks for your help Peter. 

SwarnadeepNandy
Mega Sage

Hello @chrish5,

 

To limit the characters to 30 on a field, and also exclude spaces and special characters, you can use the following regular expression:

 

^[a-zA-Z0-9]{1,30}$

 

This regular expression means:

  • ^: The start of the string or line
  • [a-zA-Z0-9]: A character class that matches any alphanumeric character (upper or lower case letters or digits)
  • {1,30}: A quantifier that specifies the minimum and maximum number of repetitions of the previous element (in this case, the character class)
  • $: The end of the string or line

This regular expression will only match strings or lines that consist of 1 to 30 alphanumeric characters, and nothing else. It will not allow any spaces or special characters, such as punctuation marks, symbols, or whitespace characters.

You can test this regular expression online using a tool like RegExr or Regex101.

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

Hi @SwarnadeepNandy,

That is actually exactly the same, only more complicated looking by splitting up [A-z] to [azA-Z] 🙂


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Essentially Yes. Just Different ways of writing.