Decimal places

More
10 months 3 weeks ago #176024 by DB1324
Decimal places was created by DB1324
Hi Everyone,


I have a survey which has several numeric input fields along the lines of 'How much did X cost?', but I can't for the life of me figure out how/where to validate the field to restrict to two decimal places (e.g. allow 12.55, but not 12.5, or 12.5555555)

I've been looking at RegEx I can't get it to work; I've tried putting it in 'Validation' ('/[0-9]+(\.[0-9][0-9]?)?/'), and in 'Question validation equation' (regexMatch('/[0-9]+(\.[0-9][0-9]?)?/')), but no joy.

We're running version 2.65.1+170522

Any help much appreciated - and apologies in advance if this is a stupid question.

Thanks

Please Log in or Create an account to join the conversation.

LimeSurvey Partners
More
10 months 3 weeks ago #176027 by tpartner
Replied by tpartner on topic Decimal places
Try this as the question validation equation:

regexMatch("/^\d+(\.\d{1,2})?$/", self)

It will allow either an integer or enforce precision to two decimal places.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago #176029 by DB1324
Replied by DB1324 on topic Decimal places
Perfect, thank you so much.

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago #176032 by DenisChenu
Replied by DenisChenu on topic Decimal places
If you need to disable 12.5 , and allow only 12.50 : you need a text question type (since 12.50 === 12.5 in numeric).

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
An error happen ? Before make a new topic : remind the Debug mode .
The following user(s) said Thank You: tpartner

Please Log in or Create an account to join the conversation.

More
7 months 5 days ago #180629 by DB1324
Replied by DB1324 on topic Decimal places
Hello,

Sorry to resurrect an old request, but I have since identified an issue with the solution that was kindly offered up here.

regexMatch("/^\d+(\.\d{1,2})?$/", self) will indeed limit responses to integers or values with two decimal places, but if I apply it to a non-mandatory question, the field effectively becomes mandatory because null/no answer gets picked up by the validation.

Is there a way of amending this validation to allow for no response?

Thank you!

(We are still running version 2.65.1)

Please Log in or Create an account to join the conversation.

More
7 months 5 days ago #180632 by tpartner
Replied by tpartner on topic Decimal places
Try this (untested):

regexMatch("/^\d+(\.\d{1,2})?$|^$/", self)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
The following user(s) said Thank You: DB1324

Please Log in or Create an account to join the conversation.

More
7 months 5 days ago #180633 by DB1324
Replied by DB1324 on topic Decimal places
Tony, that's brilliant - thank you so much!

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!