2

I am having a form in my page and I am using the validation jquery plugin from here http://jqueryvalidation.org/

My form is:

 <form action="sendform.action" id="myform" method="post" name="myform">
  <fieldset>
       <label>Name:*</label> 
       <input id="cname" name="cname" type="text" value="" required/>
  </fieldset>
 <fieldset>
       <label>Email:*</label> 
       <input id="cmail" name="cmail" type="text" value="" required/>
  </fieldset>
  <fieldset>
       <label>Phone:*</label> 
       <input id="cphone" name="cphone" type="text" value="" required/>
  </fieldset>

  <div class="submit-button">
    <a href="#">Send</a>
  </div>
 </form>    

and my javascript:

$(".submit-button").click(function(){

    /*$("#myform").valid();*/
    $("#myform").validate({
        rules: {
            cphone: {
              required: true,
              number: true
            }
        },
        submitHandler: function(form) {
            $(this).submit();
        }
    });
});

The problem is that this thing is not working. I am not getting any error logs so I do not know what is wrong. Also if I use $("#myform").valid(); instead it works. So is only the .validate() that is not working. Anyone had an idea? Thanks in advance

Sparky
  • 98,165
  • 25
  • 199
  • 285
novellino
  • 1,069
  • 4
  • 22
  • 51

1 Answers1

3

The validate() function is the initialization method for tying the validator to your form and setting its rules. The valid() function executes the validation and returns true or false depending on if your form input values passed all of your rules.

Brett
  • 4,268
  • 1
  • 13
  • 28
  • ah thanks for this answer. So I keep both, and it works! Thanks a lot – novellino Nov 15 '13 at 13:24
  • You only have to call the `validate()` function once. So, I'd move it out of your `click()` event and put it with the rest of your view's initialization code. – Brett Nov 15 '13 at 13:27
  • Worth mentioning a few things... 1) `.validate()` method is _always required_ in order to initialize plugin. 2) `.valid()` is an _optional_ method for the rare cases in which your form does not use a `type="submit"` input button. 3) OP does not need his `submitHandler` callback function if all it contains is `.submit()`. That's the _default_ behavior and everything will work the same without the `submitHandler`. – Sparky Nov 16 '13 at 20:24