jqueryjquery-select2jquery-select2-4select2-rails

Select2.js - Search input to allow only numbers


I'm struggling with this for a day now, probably is really easy to solve.

What I want is the search field to only accept numbers, so I tried something like this:

enter image description here

function onlyNumbers(){
  $('.select2-search__field').on('keypress', function () {
    $(this).val($(this).val().replace(/[^\d].+/, ""));
    if ((event.which < 48 || event.which > 57)) {
      event.preventDefault();
    }
  });
}

function select2(){
  $(".select2From").select2({
    tags: true,
    placeholder: "de",
    allowClear: true
  });
  $(".select2To").select2({
    tags: true,
    placeholder: "até",
    allowClear: true
  });
}

But that doesn't work...actually I can't even get to the '.select2-search__field'. Doesn't matter if is on keypress, on change, on click, on something...I just want to know how can I get to that input so I can filter the text on input.

Hope you can help me. Thank you!


Solution

  • For dynamically generated items, bind the event to a static DOM.

    $(".select2").select2({
        tags: true,
        placeholder: "de",
        allowClear: true
    });
    
    $(document).on('keypress', '.select2-search__field', function () {
        $(this).val($(this).val().replace(/[^\d].+/, ""));
        if ((event.which < 48 || event.which > 57)) {
          event.preventDefault();
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
    
    <select class='select2' style='min-width: 100px;'>
      <option/>
      <option>100</option>
      <option>200</option>
      <option>300</option>
    </select>