1

I'm trying to make myself a form, but I need to prevent some UX stuff depending on if a form was entered correctly. Is there an api for manually issuing the validation routine to a form's field? I know that I could listen to the post-validation events, but I actually need to start the validation from an external source.

Is this possible?

duncan
  • 31,401
  • 13
  • 78
  • 99
Athan Clark
  • 3,886
  • 2
  • 21
  • 39

3 Answers3

2

You can dispatch a change event to the form with jQuery: $("#myForm").trigger("change");
It will trigger validation.

JAre
  • 4,666
  • 3
  • 27
  • 45
  • It looks like Foundation is using the [jQuery Validator Plugin](http://jqueryvalidation.org) to implement the machinary... kinda. `$("#form").valid()` doesn't do anything. Do you know if there's any way to dig into the events running around? – Athan Clark Dec 15 '14 at 21:24
  • 1
    @AthanClark Zurb team plans to rewrite the js side of the frameworks. It's not a good idea to rally on the js inner works at the moment. – JAre Dec 15 '14 at 21:48
  • What if you were crazy? :) Do you have an idea what nut you'd try to crack? – Athan Clark Dec 16 '14 at 00:00
  • 1
    @AthanClark `Foundation.libs.abide.validate : function (els, e, is_ajax)` https://github.com/zurb/foundation/blob/master/js/foundation/foundation.abide.js#L103 – JAre Dec 16 '14 at 00:47
  • You wouldn't happen to know why this isn't working / only works for the last field that needs to be validated? – Cayce K Jul 20 '15 at 19:19
0

The accepted trigger on the form did not work for me. Instead using $('#myForm').submit() successfully triggers the checks manually. If you're using an asynchronous form you would just need to do the right preventDefault and return false on the form so that it does not submit like a normal form. I solved this by adding a universal class / data-attribute called data-ajax-submit or ajax-submit for my forms.

Cayce K
  • 2,288
  • 1
  • 22
  • 35
0

Based on the code from Foundation 5.5.3 you can try this one:

$('#my_form').trigger('validate.fndtn.abide');