0

I have an AJAX form where I have tons of text fields. These text fields validate just fine with no issues however as soon as I add a checkbox via MVC framework(resulting HTML code bellow).

<input id="NotifyLocation" class="check-box" type="checkbox" value="true"
 name="NotifyLocation" data-val-required="The NotifyLocation field is required." 
 data-val="true" checked="checked"></input>

jQuery plugin throws an error from within this code inside of the plguin.

attributeRules: function(element) {
    var rules = {};
    var $element = $(element);

    for (var method in $.validator.methods) {
        var value;
        // If .prop exists (jQuery >= 1.6), use it to get true/false for required
        if (method === 'required' && typeof $.fn.prop === 'function') {
            value = $element.prop(method);
        } else {
            value = $element.attr(method);
        }
        if (value) {
            rules[method] = value;
       //ERROR is thrown on the next line
        } else if ($element[0].getAttribute("type") === method) {
            rules[method] = true;
        }
    }

    // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
    if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
        delete rules.maxlength;
    }

    return rules;
}

The error message is "TypeError: $element[0] is undefined". I have tried adding the following code to force jQuery to ignore the checkbox in my ready method

 $("#frmAjaxLocationUpdater").validate({
        ignore: "#NotifyLocation *"
    });

but still I get the same error within jQuery validation plugin. I have tried to add rules by class still same issue. According to numerous posts this should work but it doesn't.

Any ideas what is going on ? What can cause this?

Thank you.

11
  • can you show demo of error on jsfiddle.net or jsbin.com And your code not plugin code.. Commented May 23, 2013 at 15:59
  • Not sure what do you mean by my code. My code is $("#frmAjaxLocationUpdater").validate({ ignore: "#NotifyLocation *" }); Commented May 23, 2013 at 16:13
  • Try ignore: "#NotifyLocation". Adding * only matches descendants, not the element itself. Commented May 23, 2013 at 16:21
  • Same thing same error "TypeError: $element[0] is undefined" Commented May 23, 2013 at 16:23
  • Does your MVC framework actually create potentially invalid HTML? <input></input> should be a self-closing element, <input/> Commented May 23, 2013 at 17:42

1 Answer 1

1

Here is a piece of JavaScript code that solved my issue

document.getElementById("NotifyLocation").setAttribute("data-val","false") ;
Sign up to request clarification or add additional context in comments.

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.