SJEaton Posted March 22, 2022 Share Posted March 22, 2022 Hi, we have a requirement in one of our PCs to enter contractual hours so the number may have a decimal point and shouldn't be above 40. We currently have a regex on it so only a number is entered but can someone assist with a regex that would cover all of our requirements (only a number, decimal point, not over 40) or is this not achievable? Thanks Sam Link to comment Share on other sites More sharing options...
James Ainsworth Posted March 24, 2022 Share Posted March 24, 2022 Hi @SJEaton ^(40.00|[1-3][0-9]?\.[0-9]{2})$ Try this to start with. The two decimal places are required so a user will have to add .00. Let me know if this is close enough or if there are some other tweaks that you would like to be added. Link to comment Share on other sites More sharing options...
SJEaton Posted March 24, 2022 Author Share Posted March 24, 2022 Thanks James Link to comment Share on other sites More sharing options...
SJEaton Posted March 24, 2022 Author Share Posted March 24, 2022 Perfect, thanks Link to comment Share on other sites More sharing options...
Steve Giller Posted March 24, 2022 Share Posted March 24, 2022 Shouldn't it be? ^(40\.00|[1-3][0-9]?\.[0-9]{2})$ Without escaping the first . you could enter the typo "40m00" and it'd work. Link to comment Share on other sites More sharing options...
Adrian Simpkins Posted May 8, 2022 Share Posted May 8, 2022 Hi All, Thank you for the regex - ideally we need a regex to be able to either enter 0 to 40, or 0.01 to 40.00 but without having to enter the decimal point - the above regex means we have to enter a decimal irrespective - is there anyway to do both in one? Thanks ! Link to comment Share on other sites More sharing options...
Steve Giller Posted May 9, 2022 Share Posted May 9, 2022 15 hours ago, Adrian Simpkins said: is there anyway to do both in one? Yes, but you really need a Regex expert here, not a Hornbill one! What you're asking for is a bunch of separate expressions: 0.0-0.9 0.00-0.99 1-39 1.0-39.9 1.00-39.99 (the [1-3][0-9]?\.[0-9]{2} part above) 40 40.0 40.00 (the 40\.00 part above) As you can (probably) see from the above, the '|' character separates the alternatives, so the expression is probably going to end up as long as this sentence. It is a much better option to standardise the way your users enter the information (i.e. always ##.##, so 00.00-40.00) but I appreciate that is not always within your control (although I bet they enter their account number and sort code in the right format every time!) There are probably ways to merge a few of the above options, but this is why you need a Regex expert! 1 Link to comment Share on other sites More sharing options...
Adrian Simpkins Posted May 9, 2022 Share Posted May 9, 2022 Hi Steve Thanks, I will see if the team are happy making everyone enter to 2 decimal points - I just know it will generate some noise from our customers having to enter 40.00 instead of just 40. Once we have a decision i will revisit the regex - thank you for the above many thanks Link to comment Share on other sites More sharing options...
Steve Giller Posted May 9, 2022 Share Posted May 9, 2022 It definitely can be done - it's just that I'm stretching my Regex abilities to get this far and even if I can create a working one it won't be optimised and may well have edge cases that fail. 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