Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Decimal places

  • DB1324
  • DB1324's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
5 years 4 months 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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 4 months ago #176027 by tpartner
Replied by tpartner on topic Decimal places
Try this as the question validation equation:

Code:
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.
The topic has been locked.
  • DB1324
  • DB1324's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
5 years 4 months ago #176029 by DB1324
Replied by DB1324 on topic Decimal places
Perfect, thank you so much.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 4 months 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 .
I don't answer to private message.
The following user(s) said Thank You: tpartner
The topic has been locked.
  • DB1324
  • DB1324's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
5 years 1 month 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)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 month ago #180632 by tpartner
Replied by tpartner on topic Decimal places
Try this (untested):

Code:
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.
The following user(s) said Thank You: DB1324
The topic has been locked.
  • DB1324
  • DB1324's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
5 years 1 month ago #180633 by DB1324
Replied by DB1324 on topic Decimal places
Tony, that's brilliant - thank you so much!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose