views:

76

answers:

2

I'm working in an ASP.NET MVC application. In that I need to save records(example: Name, Email, comments) using Json request. I don't have any form tag and submit button on my page. So I'm calling Javascript method on button click event to save my records asynchronously. I'm not able to use jQuery's validate plugin. Is there any jquery code snippet to validate fields without form tag?

Any help is appreciated. Thanks.

A: 

Why can't you use the validate plug in? I've used it with out a form no probs.

All I did, from memory, is instead of giving the plugin a form name, I gave it the class name of the bounding div.

So i put all my controls in a div and gave it a class name, for example, "MyFormDiv" and then pointed the validate plugin to not "#formName" but ".MyFormDiv" and it worked.

EDIT

<div class="MyClass">
  <input type="text" validation="email"/>
</div>

$(function() { $('.MyClass').validation(); });

This works for me no problem.

griegs
I tried using a div but didn't work. Do you have sample code? ... Thanks
Ravi
Ravi, have you called the $('div.MyFormDiv').validate()?
Misha N.
Um it's like above. Check my edit to see exact code I used.
griegs
A: 

This feature is built into the jQuery validator. But the validator requires a form. So:

  1. Add a form tag and a submit button to the page.
  2. When calling validate(), provide a submitHandler function in the options which submits the form via AJAX. As recommended, we use AjaxForm for this. There is sample code in the first link.

In addition to working correctly with the validator, this solution allows for progressive enhancement and requires close to no code on your part.

Craig Stuntz