Jump to content

Recommended Posts

Posted

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

Posted

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.  

  • 1 month later...
Posted

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 !

Posted
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!

  • Like 1
Posted

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

Posted

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.

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...