最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sweetalert2 multiple swal at the same function - Stack Overflow

programmeradmin6浏览0评论

I'd like to make a condition and call a swal for each one (Sweetalert2). But only one of the swal runs. How can I do it?

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..'
    );
  }
}

I'd like to make a condition and call a swal for each one (Sweetalert2). But only one of the swal runs. How can I do it?

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..'
    );
  }
}
Share Improve this question edited Apr 21, 2017 at 9:39 Limon Monte 54.4k48 gold badges187 silver badges219 bronze badges asked Jun 28, 2016 at 20:38 h1ghland3rh1ghland3r 1361 gold badge1 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

Update 2021:

Just make your function async and await promises from 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..'
    );
  }
}

Old answer which will not work for latest versions of SweetAlert2:

There's swal.queue(), use it for multiple modals.

Your case should look like this:

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);
发布评论

评论列表(0)

  1. 暂无评论