Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

orj's avatar
Level 3

sweetalert and form validation

Hi, i find it cool using sweetalert in my website, but then i got lost after implementing it on my form validation. If i have errors in validation the sweetalert still pops up.

here is what i have in my script

<script>
                                    $('#saveSettings').on('click',function(e){
                                        e.preventDefault();
                                        var form = $(this).parents('form');
                                        swal({
                                            title: "Success!",
                                            timer: 1000,
                                            showConfirmButton: false
                                        }, function(){
                                            form.submit();
                                        });
                                    })
                                </script>

regards, ciby

0 likes
1 reply
adunahay's avatar

I don't see anywhere in this script where you are validating the form. It looks like you are just grabbing the form and then activating a sweetalert box. you would need something like this:

if( form.isValid() ) {
   swal({...
} else {....

where isValid() is some custom function that decides if the form is completely filled.

Please or to participate in this conversation.