validationinputsweetalertsweetalert2

Sweetalert2 multiple input validation


I'm trying to have multiple forms inside mutiple modals, as I've read I have to use swal.mixin with queue, all of these forms have multiple inputs inside.

I've already done that, but can't find a way to validate all of these forms, any sugestion?

Here's my code:

    swal.mixin({
    confirmButtonText: 'Siguiente',
    buttonsStyling: false,
  }).queue([
    {
      html: 
        "<form class='formulario' action='' method='post'>" +

          "<div class='fila'>"+
            "<img src='src/images/svg/icons/person.svg' class='imagen'/>"+
            "<input id='name' class='espacio-datos' name='nombre' type='text' placeholder='Nombre' maxlength='20' required>" +
          "</div>"+

          "<div class='fila'>"+
            "<img src='src/images/svg/icons/id.svg' class='imagen'/>"+
            "<input id='ced' class='espacio-datos' name='num_ident' type='text' placeholder='Cedula' onkeypress='onlyNumbers(event)'>" +
          "</div>"+

          "<div class='fila'>"+
            "<img src='src/images/svg/icons/phone.svg' class='imagen'/>"+
            "<input id='tlf' class='espacio-datos' name='num_telef' type='text' placeholder='Telefono' onkeypress='onlyNumbers(event)'>" +
          "</div>"+
        "</form>",
      preConfirm: function () {        
        var array = {
          'nombre' : $("#name").val(),
          'cedula' : $("#ced").val(),
          'telefono' : $("#tlf").val(),
        }

        return array;
      },
    },
    {
      html:
        "<form action='' method='post'>" + 
          "<div class='main-cont'>"+
            "<span>" +
              "Por favor ingresa el codigo de verificacion NUIP "+
              "que hemos enviado a tu celular" +
            "</span>"+

            "<div class='row cuadros'>" +
              "<input id='num-1' class='inp-num' data-pos='0' type='text' maxlength='1' name='one' onkeypress='isInputNumber(event)' autofocus='autofocus'/>" +
              "<input id='num-2' class='inp-num' data-pos='1' type='text' maxlength='1' name='two' onkeypress='isInputNumber(event)'>" +
              "<input id='num-3' class='inp-num' data-pos='2' type='text' maxlength='1' name='three' onkeypress='isInputNumber(event)'>" +
              "<input id='num-4' class='inp-num' data-pos='3' type='text' maxlength='1' name='four' onkeypress='isInputNumber(event)'>" +
            "</div>" +
          "</div>"+
        "</form>",

      preConfirm: function () {
        return [
          $("#num-1").val(),
          $("#num-2").val(),
          $("#num-3").val(),
          $("#num-4").val(),
        ];
      },
    }

Solution

  • On sweetalert2 the inputValidator function is not called if the modal doesn't have any input defined.

    A way to workaround that in your case is to add the input in the mixin but then hide it using onBeforeOpen.

    Basically the mixin becomes:

     swal.mixin({
        confirmButtonText: 'Siguiente',
        buttonsStyling: false,
        input: 'text'
      })
    

    And then you add the following code to each element in the queue array to hide the input text:

      onBeforeOpen: function (dom) {
        swal.getInput().style.display = 'none';
      }
    

    You can see an implementation of that using your code here: https://codepen.io/anon/pen/xQxWMN