JQuery validation plugin requires form tag?

I want to check some asp.net text fields with the jQuery Validation plugin found at http://docs.jquery.com/Plugins/Validation , but it seems that the elements should be between the form tag. If I only have a couple of elements, I would hardly name this shape, so I would prefer that they are not wrapped inside the form element. Is there any way around this? Also, if I have two buttons on the form, the cancel and submit button, and I want the form to only confirm when the submit button is pressed, but not the cancel button, how is this done?

+7
jquery jquery-validate
source share
2 answers

I have a couple of elements, I would hardly call it a form, so I would rather they would not be wrapped inside the form element.

If the elements are not in the form tag, this is not a valid HTML document, so the behavior in the script may become unstable depending on how the browser works with garbled HTML.

Most browsers will create the form implicitly, but now you have no control over the behavior of the form. Typically, the default values ​​are a form of message aimed at the requested page URL.

The problem is that you probably don't know which selector to use in jQuery to get a link to the form ... but I suppose $("form") will do the trick.

confirm when submit button is pressed but not cancel button

The plugin intercepts and fires the form submit event. Usually cancel buttons are html input elements with a type attribute set to "reset":

 <input type="reset" value="cancel" /> 
Button

A reset will not cause the form to be submitted, and it will not result in validation.

If you use a different type of button, make sure onclick even returns false. This cancels the form submit action, but still allows you to run javasctipt yourself when the button is clicked.

+2
source share

The jquery authentication plugin requires the form element to function, so you must have form fields (no matter how few) contained within the form.

You can say that the validation plugin does not work when submitting the form, and then manually confirm the form when the correct submit button is clicked.

For example, using a class to identify the correct submit button:

 $(document).ready(function() { var form = $("form"); form.validate({ onsubmit: false, }); //Validate form only if validation submit button is clicked //Allows cancel submit buttons to function properly $('.validate', form).click(function() { if (form.valid()) { form.submit(); } else { form.validate().form(); } }); }); 
+2
source share

All Articles