0

As an exercise I have added some parsley validation to this form here http://matthewpiatetsky.com/cs401/login.html

I am now trying to add a similar validation to the form here http://matthewpiatetsky.com/cs401/writereviews.html
(I have some mouseout validation on the text area)

I am running into two issues:

  1. How can I get the form to validate before the form submits to its action? Can I run validation first, and if validation succeeds, then submit the form.
  2. How can I add data-required="true" to the jquery raty stars to force the user to fill those in?

Thanks!

LemonMan
  • 2,963
  • 8
  • 24
  • 34
  • You can prevent submit using `event.preventDefault();` in event handler if validation finds errors. – Anto Jurković Nov 20 '13 at 12:45
  • thanks! i'll try that! do you have any tips for the second question? – LemonMan Nov 21 '13 at 07:19
  • Sorry, no. I didn't work with that plugin – Anto Jurković Nov 21 '13 at 07:31
  • sorry i'm not actually sure how to do what you suggested. I don't currently have an event handler just additional validation? (I'm new to JavaScript) – LemonMan Nov 24 '13 at 23:17
  • would you clarify your question , cause I don't see any forms on your page ! only one textarea. – ProllyGeek Nov 24 '13 at 23:46
  • well yes there isn't a lot of regular inputs, but i get the values of the select2 boxes, the text area and the jquery raty stars. the answer below answered my question of how to check the input in general, but I'm not sure how to make sure each of the stars is checked, since the inputs are added after the page is loaded (so i can't add parsley to it) – LemonMan Nov 25 '13 at 00:35

1 Answers1

1

For validation before submit you can do something like:

var form = document.getElementById(“yourFormID”);

form.addEventListener("submit", function(event) {
    // do your additional validation here

    if (something wrong)
        event.preventDefault();

}, false);
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
  • thanks! that's very helpful! however, i was wondering if you could make a suggestion as to how to add validation to jQuery raty (since the stars are added after the page loads with JavaScript) – LemonMan Nov 24 '13 at 23:46