sweetalert2 在同一个函数中多次吞咽

sweetalert2 multiple swal at the same function

我想提出一个条件,并为每个 (Sweetalert2) 调用一个 swal。但只有一个 swal 运行。我该怎么做?

function validateEmail(email) {
  var regex = /\S+@\S+\.\S+/;
  return regex.test(email);
}

function validateBirth(data) {
  var regex = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
  return regex.test(data);
}

function validacao() {
  var data = document.getElementById('birth').value;
  var email = document.getElementById('email').value;
  if (!validateBirth(data)) {
    swal(
      'title..',
      'text..',
      'type..'
    );
  }
  if (!validateEmail(email)) {
    swal(
      'title..',
      'text..',
      'type..'
    );
  }
}

2021 年更新:

只需使您的函数 asyncawait 来自 Swal.fire() 的承诺:

async function validacao() {
  var data = document.getElementById('birth').value;
  var email = document.getElementById('email').value;
  if (!validateBirth(data)) {
    await Swal.fire(
      'title..',
      'text..',
      'type..'
    );
  }
  if (!validateEmail(email)) {
    await Swal.fire(
      'title..',
      'text..',
      'type..'
    );
  }
}

旧答案不适用于最新版本的 SweetAlert2:

swal.queue(),用于多个模态框。

您的案例应如下所示:

var modals = [];

// birth modal
if (!validateBirth(data)) {
  modals.push({title: 'title1', text: 'text1', ... });
}

// email modal
if (!validateEmail(email)) {
  modals.push({title: 'title2', text: 'text2', ... });
}

Swal.queue(modals);