0

The fields in my form already validate. If they're invalid, there's a span class that appears and informs the user. Likewise if they're empty.

So I thought it would be easy enough to just check for the visibility of one of these classes.

Here's my script:

$(document).ready(function(){
  if (($('.error').length) || ($('.empty').length)) {

      function validateReview(){

               document.getElementById('ReviewForm').submit();
      }

  } else {

      function validateReview(){

                alert("Please check fields and be sure to leave a review.");
      }

  }
};

And then the submit button in my form simply calls validateReview() onclick.

But it's not working how I expected, I get this as an error "validateReview is not defined". Did I declare validateReview() incorrectly, or in a local scope maybe? BIG newb here so please be aware. I know enough only to get in trouble.

2
  • You spelled validateReview() as validatReview(). Two times. Now you see what you did, right? Commented Jul 4, 2012 at 21:52
  • I spelled it wrong in the question, it is correct in the script. Thanks. Commented Jul 4, 2012 at 21:56

1 Answer 1

1

Not sure about your logic but whatevar it is I think you can use this

$(document).ready(function(){
    if ($('.error').length || $('.empty').length) {
        $('#ReviewForm')[0].submit();
    } else {
        alert("Please check fields and be sure to leave a review.");
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

Tried that. Form still doesn't submit and I'm still getting the "validateReview is not defined" error
You have to submit the form using a submit handler, how you are submiting it ?
normally it calls a submit function in the forms script I have. Other forms in this file use this document.getElementById('formID').submit(); which is placed in the onclick of the submit button.
Then you should use $('#formID').on('submit', function(e){ if ($('.error').length || $('.empty').length).... })
Actually how you are submitting your form, can you give me the code ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.