Skip to content
Advertisement

SweetAlert input validation

I want the alert to not disappear when the user is trying to submit an empty form,instead it should show some error in the same alert.I tried doing swal.showInputError(“some error”) but it gives an error that it is not valid function.I tried looking at the documentation but cannot find any…

Here is my code-

    swal({
     title:"Enter a Username",
     content: {
                element: "input",
                attributes: {
                    placeholder: "Type your username",
                    type: "text"
                     },
             },


    className: "mySwal"
    }).then((username)=>{
            //get the username
            if(username){

            }else{


            }

});

Advertisement

Answer

You can use this function:

inputValidator: (value) => {
    return !value && 'You need to write something!'
  }

This will validates if the user is writing a value.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement