如何在sweetalert2中显示ValidationError

时间:2017-11-18 08:47:54

标签: jquery sweetalert2

我正在使用System variable screenshot,如果金额字段为空,我正在尝试显示错误。

当我第一次尝试输入金额时,它没有问题。

当我第一次尝试将字段留空时,我收到验证错误。之后,当我写一个金额并单击提交按钮验证错误不会消失。它被卡住不会第二次验证。这是sweetalert2 v7.0.0。如果你能给我指路,我将不胜感激。我在哪里做错了?

<a href="#" class="refundbutton"><i class="fa fa-undo"></i> Refund</a>

$(".refundbutton").click(function(){
    swal({
      title: 'Enter Refund Details',
      html:'<label for="ipt" class=" control-label"><span class="oval">1</span> Select refund type</label><select class="form-control refund_type"><option value="4"> Credit Card </option> <option value="5"> Cash </option></select>' +
      '<label for="ipt" class=" control-label"><span class="oval">2</span> Enter refund amount</label><input type="text" class="form-control nDiscount_fixed" autocomplete="off" style="height: 34px;" value="">',
      showCancelButton: true,
      confirmButtonText: 'Submit',
          preConfirm: function (value) {
        return new Promise(function (resolve, reject) {

            if ($(".nDiscount_fixed").val() === "") {
              swal.showValidationError(
                'please enter a refund amount.'
              )
            }
             resolve([ $('.refund_type').val(), $('.nDiscount_fixed').val() ]) 

        })
      }
    }).then(function (result) {
    var swalrebateamount = parseFloat($('.nDiscount_fixed').val()).toFixed(2);

    if (swalrebateamount !="" && swalrebateamount !="0" ) { 
    //some code
    } 
    }).catch(swal.noop)
    });

1 个答案:

答案 0 :(得分:5)

您必须重置验证错误。 more details

    if ($(".nDiscount_fixed").val() === "") {
              swal.showValidationError(
                'please enter a refund amount.'
              )
    }
    else
    {
           swal.resetValidationError();
    }