Jump to content

Regex giving problems


Frank Reay

Recommended Posts

In some of our Contract fields we have a Regex like this:

^(.|){0,255}$

This is meant to limit the number of characters to 255. If it fails (ie more than 255 characters) then we output an error message. That is all good.

BUT some of our users have found that if they enter 1 character, then return and then enter another character - they get the error message. see below

image.png.d70c2600585802d6111a622242d1ba9a.png

I don't understand this or how I stop it. Can anyone help please?

Link to comment
Share on other sites

I'm not sure removing the ^ and $ will work in this case (could be wrong) but I would suggest using \s\S to match all non-whitespace and whitespace characters to achieve the same (I think this is usually the preferred way with JavaScript)

^[\s\S]{0,255}$

 

Link to comment
Share on other sites

Many thanks @Met. This is similar and achieved the same goal

^(.|\s){0,255}$

Inside the () (standard brackets) means

. (anything except a line break)

| (OR)

\s (whitespace character which includes space, tab, carriage return)

ie basically anything! Which I think is the same as [\s\S] in square brackets

I really don't like Regex!!!!

Link to comment
Share on other sites

2 hours ago, Steve Giller said:

After testing, simply removing the caret worked fine in my Instance.

image.png

image.png

Also, I noticed on a second look that the "pipe" after the period was redundant  as well - it wasn't breaking anything, it was just pointless.

image.png

The problem with this is that it will match regardless of how many characters you have without the anchors (^ $). If you try changing it to {0,5} does it error out when you put in more than 5 characters? Without the anchors it is just checking for 0 or more characters at any point in the string

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...