Validate SweetAlert button in JS

0

I would like to know how to validate if the agregar of sweetAlert button is pressed, in my condition I am handling amounts if the one you want to enter is greater than your% allowed%, let you know that you are exceeding and if choose cantidad maxima do the following process.

    if (parseInt(cantidad) > parseInt(max))
    {
        swal({
        title: "La cantidad es mayor a su cantidad maxima",
        text: "¿Seguro que desea agregarlos?",
        icon: "warning",
        buttons: ["Cancelar", "Agregar"],   });

    //condicion de validacion boton Agregar SweetAlert
{
 //siguiente proceso
}       
        }
    
asked by Pato 16.11.2018 в 17:47
source

2 answers

0

According to the documentation of SweetAlert2, it would be useful to carry out the validation (s) you need, through the callback then in the following way

function showAlert(){
  swal({
    title: 'Ingrese un valor máximo de 10',
    input: 'text'
  }).then((result) => {
    if(result.value > 10)
      swal('Valor superado D:');
    else
      swal('Todo bien :D');
  })
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.0/sweetalert2.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container mt-2">
  <button type="button" class="btn btn-primary" onclick="showAlert()">Mostrar popup</button>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.0/sweetalert2.all.js"></script>

You tell us:)

    
answered by 16.11.2018 в 17:56
0

This response is with SweetAlert 1

You declare the buttons and validate the answer of some by the name of the button

Link

swal("La cantidad es mayor a su cantidad maxima", "¿Seguro que desea agregarlos?", "info", {
  buttons: {
    cancelar: { text: "Cancelar"
    },
    agregar: {
      text: "Agregar"
    },
  },
})
.then((value) => {
  switch (value) {
 
    case "cancelar":
      swal("No agregado","","warning");
       //CODIGO DE NO AGREGADO O LO QUE QUIERAS HACER
      break;
 
    case "agregar":
      swal("Agreado", "La cantidad era mayor pero fue agregada por su solicitud", "success");
      //AQUI CODIGO DONDE AGREGAS
      break;
  }
});
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    
answered by 18.11.2018 в 20:06