Johnny
Johnny

Reputation: 1725

how to show ValidationError in sweetalert2

I am using sweetalert2 v7.0.0 and I am trying to show an error if the amount field is empty.

When I enter an amount on the first try it works without problem.

When I leave the field empty on the first try I get a validation error. After that when I write an amount and click submit button validation error doesn't disappear. it get stuck doesn't validate the second time. Here is the jsfiddle link. I would appreciate if you can show me the way. Where I am doing wrong?

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

Upvotes: 0

Views: 11151

Answers (1)

Krushnakant Ladani
Krushnakant Ladani

Reputation: 673

You have to reset validation error. more details

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

Upvotes: 6

Related Questions