I am trying to restrict a numbers (integer) entered based on the previous select one question

greetings,

i am building a form trying to restrict the numbers (integer) entered based on the previous question which is state.
for example if he choses California hell only be allowed to enter numbers between 1-100 and if he chooses Texas hell only be allowed to enter numbers between 1-200 and so on.

is there a condition for this kind of case

@alpha, is your previous question a select_one question type or a text question type?

hello @Kal_Lam, my question is select one type

@alpha, you should be able to do it as outlined in the image shared below:

In the survey tab of your XLSForm:

In the choices tab of your XLSForm:

Image 2

Data entry screen as seen in Enketo: when the entry is within the ranage

Data entry screen as seen in Enketo: when the entry is outside the ranage

Data entry screen as seen in Enketo: when the entry is within the ranage

Data entry screen as seen in Enketo: when the entry is within the ranage

Data entry screen as seen in Enketo: when the entry is outside the ranage

Reference XLSForm:

Constraint in Integer.xlsx (12.1 KB)

thank you @Kal_Lam, that was very helpful, although i did not consider the range in an excel cell will be that lengthy, apparently not all of my condition can fit in one cell, is there another way to do this maybe by using calculation type

thank you for the assistance

Hi @alpha, a cell in excel can hold 32,767 characters maximum. Is your condition bigger than this?

1 Like

hello @hakan_cetinkaya, unfortunately yes

Wow :exploding_head:

Greetings,

Certainly, you can implement conditional restrictions on the numbers entered in a form based on the previous question (state). To achieve this, you can use JavaScript to dynamically set the constraints for the number input field based on the selected state. Here’s a simplified example using HTML and JavaScript:

htmlCopy code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dynamic Number Restriction</title>
</head>
<body>

  <form>
    <label for="state">Select State:</label>
    <select id="state" onchange="updateNumberConstraint()">
      <option value="California">California</option>
      <option value="Texas">Texas</option>
      <!-- Add more states as needed -->
    </select>

    <br>

    <label for="numberInput">Enter Number:</label>
    <input type="number" id="numberInput" min="1" max="100">
  </form>

  <script>
    function updateNumberConstraint() {
      var stateSelect = document.getElementById("state");
      var numberInput = document.getElementById("numberInput");

      // Set different max values based on the selected state
      if (stateSelect.value === "California") {
        numberInput.max = 100;
      } else if (stateSelect.value === "Texas") {
        numberInput.max = 200;
      }
      // Add more conditions for other states as needed
    }
  </script>

</body>
</html>

In this example, the updateNumberConstraint function is called whenever the user selects a state. Inside this function, it checks the selected state and dynamically updates the max attribute of the number input field accordingly.

Feel free to adapt this code to suit the specific requirements of your form, and add more states and conditions as needed.