Validation criteria

Good morning Everyone
I’m a new Kobotoolbox user and I have a problem
I’m creating a collect app and i want to make a validation criteria on a question
In fact i would like to create this synthax : I the answer to Question x (numeric) is superior to the previous question y, do not validate and show the error message

Bonjour à toute la communauté
Je suis débutant dans l’utilisation de Kobotoolbox et j’ai un problème
En effet, dans la création de mon questionnaire je voudrais créer un critère de validation : la synthaxe que je voudrais créer est la suivante : Si la réponse à la question x (qui est numérique) est supérieur à la réponse de la précédente question y, ve pas valider ladite réponse et afficher le message d’erreur.

Au secours chers frères

Hi @gutenberg2015

Here’s what you do:

First question in your form: “Please enter a number” - this question is an “integer” question (numeric).

Second question in your form: “Please enter a smaller number” - question question is another “integer” question, and you want the form to show an error if they enter a bigger number than what they entered previously.

I don’t know what you want the form to do if the enter the SAME number in the second question?

Your validation code (or “constraint” if you’re creating it in the XLSForm) should be:
question2<question1

so, question2 must be less than the value of question1.

If it’s okay if they enter the same number, then the code would be:
question2<=question1

Hope that helps!
Janna

3 Likes

Thanks Janna. I have a follow on question if that is ok.

We aska bout the number of siblings the respondent has (Question 1), then in subsequent questions ask (how many older brothers (Q2), how many younger brothers (Q3) etc. What would be the best way to check that Q2 and Q3 combined equal Q1?

I haven’t used calculate before, so don’t know if that is the right way to measure this.

Thanks!

Hi Janna
Thank you very much, it has work
Thanks!

1 Like

Other thing is to know the URL of my projet, I’m trying http/kc.kobotoolbox.org/projet-title
it does not mork

Please see this tutorial.
And also see this: Re Validation logic based on answer to previous question

Davids also try this:
Say the Questions are as follows

  1. How many Siblings do you have ? - Number
  2. How many Older brothers do you have - Number
  3. How many Younger brothers do you have - Number.

On Question 2 validation criteria : The answer should be
1. Less than total siblings in Question 1
2. Less than or equal to Total siblings minus Younger siblings

Validation Criteria should be : .<=${TotalSiblings} and .<=(${TotalSiblings}-${YoungerSiblings})

On Question 3 validation criteria : The answer should be
1.Less than total siblings in Question 1
2 Less than or equal to Total siblings minus Older siblings

Validation Criteria should be : .<=${TotalSiblings} and .<=(${TotalSiblings}-${OlderSiblings})

@david_shields,

@bcl gave a good start to answering your question in this thread (see above).
However, that validation criteria will only check on “Question 3” if “YongerSiblings” is “less than” or equal to total-older.

It won’t check if it matches or adds up to the exact number. (so for example, you could enter “5” total siblings, “1” older, and “3” younger - and that validation will say “ok”.

But I think what you want is that the person must enter “5” total siblings, then “1” older" and “4” younger for it to be valid. Is this correct?

So say you only have three questions:

  1. Total number of siblings
  2. older siblings
  3. younger siblings.

On older siblings (question2), the only validation criteria you need is:
.<=${TotalSiblings}

On younger siblings, you then need this criteria:
.<=${TotalSiblings} and .=(${TotalSiblings}-${OlderSiblings})

Hope that helps!
Janna

5 Likes

Hi @gutenberg2015
For the URL of your project, what are you trying to do? Are you trying to connect via a tablet/smartphone? Or share with someone else?

@janna

Yes that is great way to check for exact match…
The answer i provided above allows some flexibility which i thought was necessary

Champion! Thanks Jenna and bcl

1 Like

Hello @janna

Is there a validation crateria can allow just 10 digits to fill in the string questions? since the numaric questions just allows 9 digits.

Thanks!

Hi @Osama
The code is:
regex(.,'^([a-z]){10}$')
See details here:
http://support.kobotoolbox.org/creating-forms/formbuilder/restricting-text-responses-with-regular-expressions

1 Like

Hi @Osama ,
Yes, to allow for 10 digits, you need to change the question type to “text”.
And then, as @Suman has already said, you need to have a “regex” in the validation criteria (or “constraint” column, if you’re creating the form in Excel).

For example, you might want to collect 10 digits to collect a phone number.

If you wanted to collect a 10-digit phone number (so, you want to collect 10 digits, not alphabet letters), then your validation criteria/constraint would be this:
regex(., '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

If you want to make sure the first digit they enter is a “zero”, then you could do this:
regex(., '[0][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

If some phone numbers have 9 digits, and some have 10 (like, in some countries, if you’re close to the border, then some people have phone numbers from a different country, so the length of numbers might vary), then you can put a validation like this:
regex(., '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]?')

Those are some options that I hope will help you!
Janna

3 Likes

Hi Suman,

Thank you so much for your cooperation.

Hope to work with each other in future…

1 Like

Always 10 digits, or up to 10 digits? If it must always be exactly 10 digits then @janna’s response is basically correct, although it can be somewhat simplified further to:

regex(.,'^[0-9]{10}$')

If instead it is up to 10 digits (but at least 1), then use:

regex(.,'^[0-9]{1,10}$')

Please note, the '^" and ‘$’ is required in this case, because otherwise “1234567890123456789” will match (!) Why? because despite being far too long, regex() will find a suitable ten-digit substring to achieve a match. Adding ‘^’ and ‘$’ forces it to match the entire string.

2 Likes

Thank you so much

1 Like

Hi @Suman

I have a form with Arabic as one of the languages and it displays from left-to-right when using Ona’s enketo as shown in the screenshot below instead of right-to-left.

Thanks in Advance!