0

A newbie to jQuery. I am attempting use an array to auto populate elements for form validation. For some reason data.result[1].form_field leads to an "Uncaught SyntaxError: Unexpected token ." error.

    var $Form = $("#form").validate({

        // Rules for form validation
        rules : {
            data.result[1].form_field : {
                required : true,
                range: [data.result[1].minimum, data.result[1].maximum]
            }
        },

        // Do not change code below
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent());
        }
    });

Strangly, if I directly write in the field name things work fine.

    var $Form = $("#form").validate({

        // Rules for form validation
        rules : {
            some_field : {
                required : true,
                range: [data.result[1].minimum, data.result[1].maximum]
            }
        },

        // Do not change code below
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent());
        }
    });

Not sure why data.result[1].minimum, data.result[1].maximum work, but data.result[1].form_field does not.

1
  • Is data.result defined somewhere above this? and what does the data structure look like? Commented Aug 10, 2014 at 3:09

2 Answers 2

1

Try defining the rules like so if its a dynamic key you cant declare it like that...

var specificRules = { rules: {} };

specificRules.rules[ data.result[1].form_field ] = {
     required : true,
     range: [data.result[1].minimum, data.result[1].maximum]
}

var $Form = $("#form").validate( specificRules );
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Scott. Your answer worked like a charm, however, in my original question I left some code out as I though it would make my question more easy to read. I'm not sure how to fit this additional code in your solution. Also can your solution be used in a for loop? I would like to use $.each(data.result, function (index, item) { to populate all fields.
Something like this? This won't run obviously because im not including the validate plugin but the syntax should be right... jsfiddle.net/eL1vmf4t
Works for the first field in my array, however, not for subsquent fields. I think that $qaForm.validate( currentRuleSet ); needs to be out of the loop. First the list of rules must be built before validate.
Not sure what the plugin expects as far as syntax for multiple fields. If you tell me the plugin I could tell
I've added an example of the plugin syntax. Note this is an example from some random form, but it should give you an idea of what the plugin is looking for. jsfiddle.net/eL1vmf4t/3
|
1

"Uncaught SyntaxError: Unexpected token ."

The "unexpected token" is the first dot in the field name.

As per documentation, you must enclose field names in quotes when they contain brackets or dots...

var $Form = $("#form").validate({    
    rules : {
        "data.result[1].form_field": {
            required: true,
            range: [data.result[1].minimum, data.result[1].maximum]
        }
    },
    ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.