oTree Forum >

Limiting the number of digits subjects can type

#1 by galbitton


I would really appreciate your help with limiting the number of digits subjects can type. I succeeded in limiting the maximum number of digits but not the minimum. 

This is how I defined the variable:

zipcode = models.StringField(label="Which ZIP code do you live in?", min_length=5,max_length=5)

Thanks in advance to anyone who can share their insights with me.

#2 by gr0ssmann

I would use dynamic field validation:

    def zipcode_error_message(player, value):
        if len(value) != 5:
            return "Your zip code must have 5 characters."

More info here: https://otree.readthedocs.io/en/latest/forms.html#dynamic-form-field-validation

Note that if your zipcode is truly numeric, you could do:

    zipcode = models.IntegerField(label="Which ZIP code do you live in?", min=10000, max=99999)

#3 by galbitton

Thanks! It works perfectly!

Regarding your second suggestion, that was my initial thought. But someone's ZIP code can start with 0 (such as 01584), which I found problematic to define with min and max.

Thank you again.

Write a reply

Set forum username