swal form validation before submit

See original GitHub issue

Hello SweetAlert creator,

I first of all want to congratulate on developing a beautiful and easy to use pop-up script.

For the past week I have tried to use Swal and jquery form validation examples to validate input before submitting a form and I have reached a big brick wall and need to reach out for help.

I have a swal pop-up on my website (http://plantswap.com.au) that appears when you click on “Tell a friend” button. The SWAL componnent works fine, but it submits the form even if I haven’t entered any input. Also, I am not sure that anything actually happens on submit. When submit is clicked, a php file is called through AJAX that should send an email.

Here is the javaScript in my header.php file:

–>

    <script>

         document.querySelector('div.sweetpop1 button.tell').onclick = function(e){
         e.preventDefault();
            swal({
                title: "Tell your friends!",
                text: '<form role="form" id="contact-form" method="post"><div class="control-group"><label for="friend_name">Your friend\'s name</label><input type="text" placeholder="Your friend\'s full name" id="friend_name" name="friend_name" required></div><div class="control-group"><label for="email">Email</label><input type="email" placeholder="Your friend\'s Email" id="email" name="email" required></div></form>',
                html: true,
                showCancelButton: true,
                showConfirmButton: true,
                confirmButtonText: "Submit",
                function( confirmed ) {
                        if( confirmed )
                            $(this).closest('form').submit();
                }
                });
            };


    $('#contact-form').on('submit', function(e){
        e.preventDefault();
        var myForm = $("#contact-form");
        if(myForm.valid()){

            var theData = {
                 friend_name: $('#friend_name').val(),
                 email: $('#email').val(),

            };

            $.ajax({ // ajax call starts
                url: "http://plantswap.com.au/wp-content/themes/flatads/tell_a_friend.php",
                type: 'post',
                data: theData,
                success: function(data)
                {
                    swal("Your request has been received!", "We will contact you for confirmation soon", "success");
                    document.getElementById('friend_name').value='',
                    document.getElementById('email').value='';

                }

            });
        }
    });
</script>

if you test this, you will see the swal pops up but the validation does not work. I even tried using the tramero SWAL addition (swal.forms) but the alignment and horizontal and vertical scroll bars were all wrong, so I gave up on that.

Can you please help me modify the above source code to get the form validation working, as well as follow through on the submit so the parameters are passed to my PHP file via the submit POST?

Thanks in advance.

Kind regards, Henry

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
grinsteindavidcommented, Sep 24, 2016

this is working for me

$('.swal-article-create').click(function(e, params) {
    var localParams = params || {};

    if (!localParams.send) {
      e.preventDefault();
    }

    swal({
        title: "Desea continuar?",
        text: "Se creara un nuevo articulo con la informacion proporcionada.",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#6A9944",
        confirmButtonText: "Confirmar",
        cancelButtonText: "Cancelar",
        closeOnConfirm: true
      },
      function(isConfirm) {
        if (isConfirm) {
          $(e.currentTarget).trigger(e.type, {'send': true});
        }
      }
    );
  });
0reactions
vipertecprocommented, Jan 21, 2020

This is how i did and it’s working fine

  Swal.fire({
                    title: 'Submit your Github username',
                    input: 'textarea',
                    inputAttributes: {
                        autocapitalize: 'off'
                    },
                    showCancelButton: true,
                    confirmButtonText: 'Look up',
                    showLoaderOnConfirm: true,
                    inputPlaceholder: "Write your reason to flag this user.",
                    preConfirm: (result) => {
                       if(result === null || result === '' || result === undefined){
                           Swal.showValidationMessage(
                               `Request failed: Reason is required`
                           );
                       }
                    },
                    allowOutsideClick: () => !Swal.isLoading()
                }).then((result) => {
Read more comments on GitHub >

github_iconTop Results From Across the Web

sweet alert continue submitting form on confirm - Stack Overflow
$('#btn-submit').on('click',function(e){ e.preventDefault(); var form = $(this).parents('form'); swal({ title: "Are you sure?
Read more >
Simple JavaScript Form Validation using Sweet Alert and ...
In this Video we will create Simple JavaScript Form Validation using Sweet Alert and Bootstrap 4. Instead of custom JavaScript Confirm Alert ...
Read more >
sweetalert and form validation - Laracasts
If i have errors in validation the sweetalert still pops up. here is what ... preventDefault(); var form = $(this).parents('form'); swal({ title: "Success!...
Read more >
Login Form - SweetAlert2 example
Login Form. Try me! In this example, we are using the html property to create login and password inputs. Note how we are...
Read more >
Developers - swal form validation before submit - - Bountysource
For the past week I have tried to use Swal and jquery form validation examples to validate input before submitting a form and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found