Frank Reay Posted January 23, 2024 Posted January 23, 2024 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 I don't understand this or how I stop it. Can anyone help please?
Steve Giller Posted January 23, 2024 Posted January 23, 2024 I think this is because your REGEX states "A line of up to 255 characters" and the input is "Two lines, not exceeding a total of 255 characters" so I'd suggest removing the initial caret.
Met Posted January 23, 2024 Posted January 23, 2024 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}$
Frank Reay Posted January 23, 2024 Author Posted January 23, 2024 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!!!!
Steve Giller Posted January 23, 2024 Posted January 23, 2024 After testing, simply removing the caret worked fine in my Instance. 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.
Met Posted January 23, 2024 Posted January 23, 2024 2 hours ago, Steve Giller said: After testing, simply removing the caret worked fine in my Instance. 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. 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
Frank Reay Posted January 24, 2024 Author Posted January 24, 2024 Met is correct. I did try (.){0,255}$ and it does not work. I can enter over 255 characters and it is not rejected. I really really don't like Regex!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now