9

How can I validate some inputs that are not inside a form tag?

All the samples I found have a form tag, but I want to validate some inputs that are not inside a form.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Bruno
  • 4,337
  • 12
  • 42
  • 55

2 Answers2

11

The previously accepted answer (since deleted) was linking to a plugin that was last updated in January 2009. This obsolete plugin is very different than the plugin the OP was asking about:

Title: "Validate inputs that are not inside a form with jQuery Validation plugin"

Tag:

Using the jQuery Validate plugin, you cannot validate input elements that are contained outside of a <form></form> container. There is no workaround for this limitation.

DEMO 1: Shows the proper usage with a form container.

<form id="myform">
    <input type="text" name="myinput" />
    <input type="submit" />
</form>

http://jsfiddle.net/nswnomn6/


DEMO 2: Shows the form container changed into a div and the same code is now totally broken. The plugin does nothing without a form container.

<div id="myform">
    <input type="text" name="myinput" />
    <input type="submit" />
</div>

http://jsfiddle.net/nswnomn6/1/


Your only alternative would be to use a different jQuery plugin for form validation. Unfortunately, I know of no jQuery form validation plugin that would allow you to validate input elements outside of a <form> container.

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I took the accepted answer as an alternative plugin since the one mentioned can't do the job. The alternative plugin does still work in the latest version of jQuery, even though it is 5 years old. Though, it would have been nice if the original answer clarified that the plugin suggested isn't the same as the one tagged. – Kevin B Oct 06 '14 at 22:01
2

Provided we're talking about the same validation plug-in, I don't think it's possible.

I think the requirements for the plug-in dictate that the items to be validated are inside a form.

You could always try replacing the form with a div in a sample page...

Kieron
  • 26,748
  • 16
  • 78
  • 122
  • I tried using a div but didn't work... thanks! – Bruno Jan 28 '09 at 12:54
  • Ah, in that case it's probably tied to the form then - sorry and good luck. – Kieron Jan 28 '09 at 13:21
  • 2
    **This is the correct answer.** Using jQuery Validate, you can only validate fields _within_ a `form` container. – Sparky Oct 03 '14 at 17:42
  • it might be the correct answer, but it's actually more useful to tell what can be done if the plugin OP is using doesn't do the job, so the optimal answer would probably be this combined with other alternatives (such as the ones proposed by others). – eis Oct 05 '14 at 08:21
  • @eis, agreed. I posted a new answer. – Sparky Oct 05 '14 at 16:29