Welcome to the LimeSurvey Community Forum

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

Validate comment field in multiple choice with comments

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago - 3 years 3 months ago #224802 by eniisula
I want the user to add in the comments only numbers from 0 to 100. Also I want the sum of all my comment field to be 100. 
How can I achieve this?
Last edit: 3 years 3 months ago by eniisula.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #224811 by Joffm
Hi,
I always advice:
Activate your survey and have a look at the answer table.
Here you see how you can access your variables.
Here you see
 

The value of the checkbox is stored in
Q1_SQ001, Q1_SQ002
and the text input is stored in
Q1_SQ001comment, Q1_SQ002comment

And then you may deactivate again.

Now it is easy to validate.
The first part (numbers between 0 and 100) is a bit long, because you have to check each subquestion, like
(is_empty(Q1_SQ001comment) OR (is_numeric(Q1_SQ001comment) AND Q1_SQ001comment ge 0 AND Q1_SQ001comment le 100)) AND
(is_empty(Q1_SQ002comment) OR (is_numeric(Q1_SQ002comment) AND Q1_SQ002comment ge 0 AND Q1_SQ002comment le 100)) AND
...


The second validation is easy. You only have to add
AND sum(self.sq_comment)==100

And you can shorten this considering that 
  • all numbers greater or equal 0
  • sum equal 100
is sufficient.
So you can't enter a number greater 100 without breaking the second validation.

Please read the manual about implemented functions and the "self", "this" and "that" variables.
[url] www.limesurvey.org/manual/ExpressionScri...mplemented_functions [/url]
[url] www.limesurvey.org/manual/ExpressionScri....22that.22_variables [/url]

And you may study the sample surveys.
Either download and import them
[url] www.limesurvey.org/manual/ExpressionScript_sample_surveys/en [/url]

or use the demo installation of LimeSurvey (no credentials necessary)
[url] demo.limesurvey.org/index.php?r=admin [/url]
All these samples are there in group "default".

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 3 months ago by Joffm.

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #224822 by eniisula
(is_empty(G03Q24_S1comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK)) and (is_empty(G03Q24_S1comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK)) and (is_empty(G03Q24_S3comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S3comment.NAOK)) and (is_empty(G03Q24_S4comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S4comment.NAOK)) and (is_empty(G03Q24_S5comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S5comment.NAOK)) and sum(self.sq_comment)==100

I implemented like this and it works fine when I remove this line sum(self.sq_comment)==100

What shoudl I do?

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #224824 by Joffm
I do not see any issue,
except these wrong doubles
(is_empty(G03Q24_S1comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK)) and
(is_empty(G03Q24_S1comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK)) and





Another advice.
It is a good idea to insert your validation equation in the question text (with surounding { } ).
You may split the equation into several parts to see better what works, what does not.

Joffm

And as I said:
This would be sufficient
Q1_S1comment.NAOK ge 0 and Q1_S2comment.NAOK ge 0 and Q1_S3comment.NAOK ge 0 and Q1_S4comment.NAOK ge 0 and Q1_S5comment.NAOK ge 0 and sum(self.sq_comment)==100

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 3 months ago by Joffm.

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago - 3 years 3 months ago #224892 by eniisula
"The second validation is easy. You only have to add
AND sum(self.sq_comment)==100"


Could you show me how to include this in my tip which is so far like this:

{if(is_empty(G03Q24_S1comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S1comment.NAOK), '' , 'Vetem numra nga 1 deri 100!')}
{if(is_empty(G03Q24_S2comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK), '' , 'Vetem numra nga 1 deri 100 pranohen!')}
{if(is_empty(G03Q24_S3comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S3comment.NAOK), '' , 'Vetem numra nga 1 deri 100 pranohen!')}
{if(is_empty(G03Q24_S4comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S4comment.NAOK), '' , 'Vetem numra nga 1 deri 100 pranohen!')}
{if(is_empty(G03Q24_S5comment.NAOK) or regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S5comment.NAOK) , '' , 'Vetem numra nga 1 deri 100 pranohen!')}


While my question validation equation goes like this:

(
is_empty(G03Q24_S1comment.NAOK)
or
regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S1comment.NAOK)
) and
(
is_empty(G03Q24_S2comment.NAOK)
or
regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S2comment.NAOK)
) and
(
is_empty(G03Q24_S3comment.NAOK)
or
regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S3comment.NAOK)
) and
(
is_empty(G03Q24_S4comment.NAOK)
or
regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S4comment.NAOK)
) and
(
is_empty(G03Q24_S5comment.NAOK)
or
regexMatch('/^[1-9][0-9]?$|^100$/', G03Q24_S5comment.NAOK)
) and sum(self.sq_comment)==100"


Thank you in advance!
Last edit: 3 years 3 months ago by eniisula.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #224894 by Joffm
With your validation tip you might get this
 

You obviously don't want to display this.

Better to use a nested IF.
Start slowly:
The syntax of the IF-statement is
if(condition, true part, false part)
And of course you can nest this, like
if(condition1, true part 1, if(condition2, true part 2, false part 2))
So the blue part is handled if the first condition is FALSE.
And you can do this many times.

Now the validation equation:
Here you enter which conditions have to be TRUE
And in the validation tip you usually display something if the condition is FALSE.

In your validation equation you have AND operators.
A and B and C and D and E and F  (with F as the "sum" condition.
A, B, C, D, E, F have to be TRUE that the entire validation is TRUE

When will this equation be FALSE?
Either you say
NOT(A and B and C and D and E and F) 
or you use this simple transformation
NOT A  or NOT B or NOT C or NOT D or NOT E or NOT F
as we learned at school.

Now you can write your tip like (remember: the exclamation mark is the NOT operator)
{if(!A or !B or !C or !D or !E,"Only numbers 0-100",if(!F,"Sum must be 100",""))}

Replace A,B,C,.. by the conditions and it's fine. Don't forget the .NAOK!

By the way: It is not easy to validate this. You expect a certain value, but the cell is empty at the beginning, and the sum is not equal 100 at the beginning. So there is always an initial error.

I found that it might be better to take into account the checkboxes, like
(Q1_S1!="Y" or Q1_S1comment.NAOK ge 0) and (Q1_S2!="Y" or Q1_S2comment.NAOK ge 0) and (Q1_S3!="Y" or Q1_S3comment.NAOK ge 0) and (Q1_S4!="Y" or Q1_S4comment.NAOK ge 0) and (Q1_S5!="Y" or Q1_S5comment.NAOK ge 0) and sum(self.sq_comment.NAOK)==100

​​​​​​​Joffm









 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 3 months ago by Joffm.
The following user(s) said Thank You: eniisula

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #225013 by eniisula
I tried this code that you provided me:
(Q1_S1!="Y" or Q1_S1comment.NAOK ge 0) and (Q1_S2!="Y" or Q1_S2comment.NAOK ge 0) and (Q1_S3!="Y" or Q1_S3comment.NAOK ge 0) and (Q1_S4!="Y" or Q1_S4comment.NAOK ge 0) and (Q1_S5!="Y" or Q1_S5comment.NAOK ge 0) and sum(self.sq_comment.NAOK)==100

and it works better than my previous one. Thank you so much!

But I am having troubles with the question validation tip again. Could you provide me the question validation tip code for this one that you provided me. I would be really grateful if you could write it for me.

Thank you again!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #225014 by Joffm
So, what do you want to achieve?
The problem is - and you cannot avoid it - that you have an initial error in the question because of the "sum==100"-condition, which is not fulfilled as long as it is not equal 100.

In my opinion the best is to have a tip like
"The total has to be 100" which is either displayed in red as long as it is not equal 100, and then changes the color to green.
And additionally you could say:
{if(Q1_S1comment.NAOK lt 0 or Q1_S2comment.NAOK lt 0 or ...,"<br/>Only values between 0 and 100 are allowed","")}
 

 




On the other side it would be easy if you used a different type of question.
Why do people have to check the option, then enter a value.
Easier, question "multiple numerical input" 
Only enter numbers. Leave empty what you wouldn't have checked. Or use the plugin "checkBoxForText" as I did here.




 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #225015 by eniisula
{if(!(G03Q24_S1!="Y" or G03Q24_S1comment.NAOK ge 0)
or !(G03Q24_S2!="Y" or G03Q24_S2comment.NAOK ge 0)
or !(G03Q24_S3!="Y" or G03Q24_S3comment.NAOK ge 0)
or !(G03Q24_S4!="Y" or G03Q24_S4comment.NAOK ge 0)
or !(G03Q24_S5!="Y" or G03Q24_S5comment.NAOK ge 0) , "Pranohen vetem numra",if(!sum(self.sq_comment.NAOK)==100,"Shuma duhet te jete 100",""))}


This is what I wrote!
The problems I am facing are:
1. It takes numbers greater than 100
2. It does not output the sentence when the sum is not equal to 100

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #225016 by eniisula
I do not mind the initial error but I cannot seem to display the sentences like you did for example: "Total has to be 100"

Also people just enter the value and the option is automatically checked. That is not a problem.
I want this type of question (multiple choice with comments) because I have used a script that removes the field for one of my options because that is how I want it to be displayed.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #225020 by Joffm
Hi, I do not see a big issue in your solution
 



The only thing:
!sum()==100 doesn't seem to be correct; use sum()!=100

{if(!(Q1_S1!="Y" or Q1_S1comment.NAOK ge 0) or !(Q1_S2!="Y" or Q1_S2comment.NAOK ge 0) or !(Q1_S3!="Y" or Q1_S3comment.NAOK ge 0), "Pranohen vetem numra",if(sum(self.sq_comment.NAOK)!=100,"Shuma duhet te jete 100",""))}

As I recommended some time ago:
Display the condition in the question text, split it into small parts.
You see exactly what doesn't work 

And your last question.
Just play around in a test survey.
You know that the tip - without any condition, just plain text - is displayed in green if there is no error and in red if there is an error.
Now you can add certain conditions to this tip - with the IF-statement.
You see that in case the additional tip is displayed there is a <br/> at the beginning to force a new line

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • eniisula
  • eniisula's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 months ago #225021 by eniisula
I understand. Your explanation is very clear.

Now actually I want to remove the initial error. Could you help me with that please? How can I edit my validation equation to remove the initial error?

My Question Validation Equation is:

(G03Q24_S1!="Y" or G03Q24_S1comment.NAOK ge 0) 
and (G03Q24_S2!="Y" or G03Q24_S2comment.NAOK ge 0) 
and (G03Q24_S3!="Y" or G03Q24_S3comment.NAOK ge 0) 
and (G03Q24_S4!="Y" or G03Q24_S4comment.NAOK ge 0) 
and (G03Q24_S5!="Y" or G03Q24_S5comment.NAOK ge 0) and sum(self.sq_comment.NAOK)==100  


and my Question Validation Tip is:

{if(!(G03Q24_S1!="Y" or G03Q24_S1comment.NAOK ge 0)
or !(G03Q24_S2!="Y" or G03Q24_S2comment.NAOK ge 0)
or !(G03Q24_S3!="Y" or G03Q24_S3comment.NAOK ge 0), "Pranohen vetem numra",if(sum(self.sq_comment.NAOK)!=100,"Shuma duhet te jete 100",""))}



 

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose