Short answer question with a number range

Sue.S.4
Sue.S.4 Posts: 75 🧭

How do we input an answer in a short answer question which permits any number between a range - eg any number between 1000-1400. We have [1000-1400] and if you type say 1125, it marks it as incorrect.

Thanks in advance.

Best Answer

  • Jennifer.W.973
    Jennifer.W.973 Posts: 423 🎆
    edited May 22 Answer ✓

    Hi Sue, I would use this for 5 through 12: ^([5-9]|1[0-2])$

    Regular expressions find matches based on each individual digit, so [5-9] can only search for 5 through 9, then the pipe symbol | means or, while 1[0-2] searches for 10 through 12. The ^ and $ symbols are just to denote the beginning and end of the line. Without them, any of those numbers within a larger number (such as the 5 in 15 or 150) would technically be a match.

Answers

  • Jennifer.W.973
    Jennifer.W.973 Posts: 423 🎆
    edited May 22

    @Sue.S.4 Hi Sue, when creating/editing the question, click abc to open the drop-down menu and choose Regular Expression. Then paste this into the answer blank: ^(1,?[0-3][0-9][0-9]|1,?400)$

    You can remove both ,? if you don't want to allow for commas in the answer.

  • Sue.S.4
    Sue.S.4 Posts: 75 🧭

    Thank you Jennifer, I"ll try it out.

  • Sue.S.4
    Sue.S.4 Posts: 75 🧭

    Hi Jennifer, As long as I click regular expression, and the response is anywhere between 5 and 12, would this be suitable in a short answer box as a possible answer [5-12].

    Sorry I'm not a programmer - I'm an academic helping other academics…

  • Sue.S.4
    Sue.S.4 Posts: 75 🧭

    Thank you so much Jennifer. That is such a helpful response :)

  • Rusha.S.831
    Rusha.S.831 Posts: 128

    Hi @Sue.S.4,

    May I suggest trying the following regular expression in the answer field: ^(1[0-2]|[5-9])$ This may help ensure that only valid responses between 5 and 12 are accepted.

    For context, here's how it works:

    • ^ and $ are anchors that ensure the match applies to the entire string, from start to finish.
    • 1[0-2] matches the numbers 10, 11, and 12.
    • [5-9] matches the numbers 5 through 9.

    This expression will successfully match any number from 5 to 12, while excluding anything outside that range.

    Thanks,

    Rusha

  • Sue.S.4
    Sue.S.4 Posts: 75 🧭

    Thank you Rusha

    This is very helpful, especially the explanation.

    Regards, Sue