Frank Reay Posted January 23 Share Posted January 23 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? Link to comment Share on other sites More sharing options...
Steve Giller Posted January 23 Share Posted January 23 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. Link to comment Share on other sites More sharing options...
Met Posted January 23 Share Posted January 23 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 More sharing options...
Frank Reay Posted January 23 Author Share Posted January 23 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 More sharing options...
Steve Giller Posted January 23 Share Posted January 23 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. Link to comment Share on other sites More sharing options...
Met Posted January 23 Share Posted January 23 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 Link to comment Share on other sites More sharing options...
Frank Reay Posted January 24 Author Share Posted January 24 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! Link to comment Share on other sites More sharing options...
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