-1

I would like to prevent submission if form is not valid and to print some error message if possible, here's what I have so far:

btw "link to the current page" is defined in php

hbspt.forms.create({
    css: '',
    portalId: 'hs-portal-id-goes-here',
    formId: 'hs-form-id-goes-here',  
    onFormReady: function(){
        jQuery('#hsForm_hs-form-id-goes-here').validate({
            errorPlacement: function(error, element) {},
            rules: {
                firstname: { required: true },
                lastname: { required: true },
                email: { required: true },
                message: { required: true }
            },
            submitHandler: function(form, e) { 
                e.preventDefault();
                window.open('link to the current page', '_self');
                form.submit();
                jQuery(newForm)
                var newForm = jQuery('#hsForm_hs-form-id-goes-here');
                window.setTimeout(function() {
                    newForm.html('<h3>Thank you for submitting the form</h3>');
                }, 1000);
            }
       });
    }
});         
Falko
  • 17,076
  • 13
  • 60
  • 105
Darko
  • 920
  • 10
  • 22
  • This just sounds like ordinary client validation to me. Are you getting some error message that we can look at? – Robert Harvey Jul 17 '15 at 21:32
  • actually no, if form isn't valid hubspot does not receiving the data, but "Thank you for submitting the form" message is being displayed anyway – Darko Jul 17 '15 at 21:37
  • Then you need to write some client-side validation code. – Robert Harvey Jul 17 '15 at 21:39
  • agreed, I was just wondering if it's necessary to include this too http://jqueryvalidation.org/files/dist/additional-methods.min.js – Darko Jul 17 '15 at 21:41

1 Answers1

2

Do you want client-side or server-side validation?

If answer is client-side, you should do it with javascript, check inputs and write validation messages.

If it is server-side, you should consider using ajax, serialize your form, send it to server, and react depending on answer (was data successfully validated, or not)

emir-alicic
  • 97
  • 1
  • 5
  • In addition, make both (frontend-backend) validations is still better. – kosmos Jul 17 '15 at 21:51
  • In addition, the form is embedded from hubspot on external page (embedded with js), what I'm actually trying to do is to open the embedded form in modal - so I guess "back-end" or better to say server-side validation is out of the option, I just need "preventDefault()" in case form isn't valid, that's all – Darko Jul 17 '15 at 22:04
  • I am sorry, I didn't read your question at all. I only improved this answer with a short comment. Anyway seems you know what to do so I don't understand where is the problem exactly. @Darko – kosmos Jul 17 '15 at 22:39
  • @kmsdev No offense mate, actually the issue was with the validation working only once - the 1st time modal is being opened , so I wrapped up entire "hbspt.forms.create" into another function, and I call it each time the modal window is opened, that's it, thx for the comment btw :) – Darko Jul 18 '15 at 00:47