Spilot
Spilot

Reputation: 1525

why won't jquery form validation work?

I have a form that opens up inside of a dialog after a click event. I'm wondering if there is a syntax problem in my jquery that I'm not noticing. None of the logs in the submit handler function are logging.

HTML:

<div id="dialog" title="Thanks For Volunteering">
     <form>
         <p>Please provide a contact number. It will only be shared with the comic</p>
         <input id="number" name="number" type="text" />
         <button id="submit" class="button">Submit</button>    
    </form>   
</div>

JavaScript:

$("form").submit(function(){

        console.log("submit");
        $('#number').validate({
            rules: {
                  number : {
                    required: true
                    //customvalidation: true
                }                                        
            },
            messages: {
                number : {
                    required: "enter a phone number"                   
                }
            },
            submitHandler: function(form) {
                    // do other stuff for a valid form
                    //submit to parse

                    console.log("now validated");
                    var num = $('#number').val();
                    console.log("validated:" +' '+num);
            }     
        });

    });//closes submit

   $.validator.addMethod("customvalidation",
       function(value, element) {
               return (/^[A-Za-z\d=#$%@_]+$/.test(value));
       },
   "Sorry, no special characters allowed");

Upvotes: 0

Views: 605

Answers (2)

Sparky
Sparky

Reputation: 98738

The .validate() method is only the initialization of the plugin. It never goes inside of a submit() handler. Simply attach it to your form and call it once upon DOM ready.

$(document).ready(function() {
    $('form').validate({
        .....

You will also need to change your form's <button> element into a type="submit".

DEMO: http://jsfiddle.net/GJggc/1/


Please refer back to this answer on your previous question, which also addresses the two JavaScript issues above along with a detailed explanation:

https://stackoverflow.com/a/18116262/594235

Upvotes: 2

Softlion
Softlion

Reputation: 12595

$('#number').validate

You have a bug. It is the FORM element which should be validated, not a form field.

<form id="youform">

....

$('#youform').validate

Upvotes: 1

Related Questions