Short answer question with a number range

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
-
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
-
@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.
-
Thank you Jennifer, I"ll try it out.
-
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…
-
Thank you so much Jennifer. That is such a helpful response :)
-
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
-
Thank you Rusha
This is very helpful, especially the explanation.
Regards, Sue