JoelFan
JoelFan

Reputation: 38714

jquery validate() returning undefined

I'm trying to use the Validate plugin for Jquery just to validate one field. I can get it working on a simple test form but when I tried to plug it into a large existing form on my site, calling validate returns undefined. It's supposed to return a Validator object. How do I go about figuring out what's wrong?

Here is the example code that's working:

<script type="text/javascript">
function tst(ctrl) {
    var frm = $(ctrl).parents('form');
    alert('Is email valid? ' + frm.validate({ showErrors: function () { } }).element(ctrl));
}
</script>
<form id="form1" action="" method="post">
    <input type="text" name="email" onblur="tst(this)" class="email"/>
</form>

However if I try this on a much larger existing form, the validate call returns undefined, resulting in a JavaScript error calling element. I think there must be something in the larger form that makes validate "crash", but what could it be?

Upvotes: 1

Views: 10412

Answers (2)

Sergio Venicius
Sergio Venicius

Reputation: 39

did you try using JQuery object.. instead of var frm, using var $frm:

var $frm = $(ctrl).parents('form');
alert('Is email valid? ' + $frm.validate({ showErrors: function () { } }).element(ctrl));

when you set frm without $ you would just store the html element in a pure javascript variable, not getting the element in a JQuery object..

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630439

.valiate() doesn't return a boolean, it sets up validation.

To determine whether a <form> or input element is valid you call .valid() to get a boolean result. Make sure to call this after .validate(), since there will be no validator setup on the <form> otherwise.

Upvotes: 5

Related Questions