简体   繁体   中英

Uncaught TypeError: Cannot read property 'call' of undefined

I have a preexisting form which I'm trying to add jquery validation to containing :

<form id="form" method="POST" action="/title/">
    {% csrf_token %}
<input type="hidden" name="id" value="6">
<input type="hidden" name="custom_checkbox_Electronic_Signature_man" value="1">
                            <p> <span class="style9">First Name </span>
                              <input type="text" name="first_name" value="" size="20" class="style7"><span class="style9">
                            Last Name </span>
                              <input type="text" name="last_name" value="" size="20" class="style7"><span class="style9">

Using the jquery validator plugin, I have added:

$('form').validate({
    rules: {
        first_name: {
            minlen: 3,
            maxlength: 15,
            required: true
        },
        last_name: {
            minlength: 3,
            maxlength: 15,
            required: true
        },
        email: {
          required: true,
          email: true
        },
         phone1: {
             required: true,
             phoneUS: true
          },
        phone2: {
             required: true,
             phoneUS: true
          },
            street: {
            required: true
          },
              city: {
            required: true
          },
              state: {
            required: true
          },
              zip: {
            zipcodeUS: true
          }

    },
    highlight: function(element) {
        $(element).closest('.style9').addClass('.style13');
        //$(element).addClass('.style13');
    },
    unhighlight: function(element) {
        $(element).closest('.style9').removeClass('.style13');
        //$(element).removeClass('.style13');
    },
    errorElement: 'span',
    errorClass: 'style13',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});

I'm getting the error listed above . What am I doing wrong?

Replace minlen with minlength , and it works, there is no minlen property, so call fails internally

$('form').validate({
    rules: {
        first_name: {
            minlength: 3,  // <- here
            maxlength: 15,
            required: true
        },
        last_name: {
            minlength: 3,
            maxlength: 15,
            required: true
        },
   ..........

Generally speaking this is an error that can be raised if you have set a non valid jQuery validation rule.
You can find the valid validation rules using this link.

$(document).ready(function() {
    jQuery.validator.addMethod("noSpace", function(value, element){
            return value.indexOf("  ")<0 && value !==""; 
    }, "Space are not allowed");
});

try

jQuery(document).ready(function(){
                $('form').validate({
    rules: {
        first_name: {
            minlen: 3,
            maxlength: 15,
            required: true
        },
        last_name: {
            minlength: 3,
            maxlength: 15,
            required: true
        },
        email: {
          required: true,
          email: true
        },
         phone1: {
             required: true,
             phoneUS: true
          },
        phone2: {
             required: true,
             phoneUS: true
          },
            street: {
            required: true
          },
              city: {
            required: true
          },
              state: {
            required: true
          },
              zip: {
            zipcodeUS: true
          }

    },
    highlight: function(element) {
        $(element).closest('.style9').addClass('.style13');
        //$(element).addClass('.style13');
    },
    unhighlight: function(element) {
        $(element).closest('.style9').removeClass('.style13');
        //$(element).removeClass('.style13');
    },
    errorElement: 'span',
    errorClass: 'style13',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});
            });

Make sure that you did not include the jQuery validation script twice. I had this done, I have included it in the Layout and the Create views, which makes the problem. I removed it from the create view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM