admin管理员组

文章数量:1287849

I am using jquery validate /

I have a form with a first field added manually (hard coded)

Then adding more fields is done programatically. The ARE appended inside the form tags already, into a div #to-append

The one added manually validates fine.

For the rest, I am trying to add the rules like this, every time i add a field:

var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id

$($largestID).rules("add", {
        required: true,
        minlength: 3,
        pattern: yt

});

But get the "Uncaught TypeError Cannot read property 'form' of undefined" error stated above.

Any help would be appreciated.

I am using jquery validate http://jqueryvalidation/

I have a form with a first field added manually (hard coded)

Then adding more fields is done programatically. The ARE appended inside the form tags already, into a div #to-append

The one added manually validates fine.

For the rest, I am trying to add the rules like this, every time i add a field:

var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id

$($largestID).rules("add", {
        required: true,
        minlength: 3,
        pattern: yt

});

But get the "Uncaught TypeError Cannot read property 'form' of undefined" error stated above.

Any help would be appreciated.

Share Improve this question asked Feb 18, 2016 at 5:46 user3808307user3808307 1,47112 gold badges63 silver badges113 bronze badges 6
  • The rules should be added to the DOM elements or the tags. You are adding to ID. – Sravan Commented Feb 18, 2016 at 5:56
  • What is the place of this js code in your script, before form or after form? – PHPExpert Commented Feb 18, 2016 at 5:59
  • @Sravan I tried with name it still doesn't work – user3808307 Commented Feb 18, 2016 at 6:03
  • @ PHPExpert I am including it at the end before the </body> – user3808307 Commented Feb 18, 2016 at 6:04
  • @Sravan I get a different error: Uncaught Error: Syntax error, unrecognized expression: url[] . My names are an array, how can i do it then? – user3808307 Commented Feb 18, 2016 at 6:20
 |  Show 1 more ment

2 Answers 2

Reset to default 4

You are adding rule taking ID string. Instead take the element selector, and add rule to that selector.

var $largestID = $("#to-append > div:last > div:first > input:first").attr("id");
console.log($largestID); //throws right id

$('#'+$largestID).each(function () {
    $(this).rules('add', {
       required: true,
       minlength: 3,
       pattern: yt
    });
});

Use the $('#'+$largestID) to get the field of that ID, and then add rules.

To add rules to array, use:

 $('#'+$largestID).each(function () { })

you can even validate the array of names of that element:

 $('#'+$largestID).validate({
    rules: {
      'url[]': {
        required: true,
      }
    }
});

There's no need to use the ID. You have a selector that returns the element, add the rule to that directly.

$("#to-append > div:last > div:first > input:first").rules("add", {
    required: true,
    minlength: 3,
    pattern: yt

});

本文标签: javascriptjQuery validate Uncaught TypeError Cannot read property 39form39 of undefinedStack Overflow