Questions tagged [jquery-validate]

The jQuery Validate plugin is a jQuery plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.

The jQuery Validate plugin is a plugin by Jörn Zaefferer. Its purpose is to perform client-side form validation of user entered data.

Helpful Links:


Stack Snippet Starter Pack:

HTML - Include the plugin script someplace after the jQuery library:
(Use CDN links or host the scripts yourself)

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.js"></script>

<form id="myform" action="post.php">
  <input type="text" name="first_name" /><br/>
  <input type="text" name="last_name" /><br/>
  <input type="text" name="phone" /><br/>
  <input type="submit" />
</form>
$(document).ready(function() {  // <-- ensure form's HTML is ready

  $("#myform").validate({  // <-- initialize plugin on the form.
    // your rules and other options,
    rules: {
      first_name: {  // <-- this is the name attribute, NOT id
        required: true
      },
      last_name: {
        required: true
      },
      phone: {
        required: true,
        digits: true
      }
    }
  });

});

jsFiddle Demo: http://jsfiddle.net/2nhcfkLj/

Documented Options: http://jqueryvalidation.org/validate


Helpful questions:

Other typical issues:

  • All input elements to be validated must be enclosed within a set of <form></form> tags. The only elements that can be validated are select, textarea, certain input types, and certain elements containing the contenteditable attribute.
  • Rules are defined by input name attributes, not by id, when declared within the rules option of .validate().
  • all input elements to be validated must contain a unique name attribute. (All radio or checkbox elements within a single "grouping" may share the same name as this one grouping is considered a single data point. However, each grouping must contain a unique name.)
  • .validate() should be called once within DOM ready to initialize the plugin. Optionally use .valid() to test the form for validity and get a boolean result of that test.
  • There is no need to enclose .validate() inside of any click or submit handler. The plugin will automatically capture and handle the submit button.
  • A name with certain special characters must be enclosed in quotes when declared within the rules option of .validate().
  • Use the submitHandler callback function to deal with successfully validated forms and/or submit via ajax.
  • Use the invalidHandler callback function for invalid forms.
  • If using the highlight or unhighlight callback function, be sure to also include the other one. They are complementary and should be used together for best results.
  • By default, the plugin will ignore any hidden input elements. This can be prevented by setting the ignore option to ignore: [] (ignore nothing; validate everything).
  • If you have multiple submit buttons where one (such as "save") needs to bypass validation but still needs to submit the form data, use class="cancel" on the button.

Related tags

6769 questions
81
votes
18 answers

How can I use jQuery validation with the "chosen" plugin?

I have some
Omu
  • 69,856
  • 92
  • 277
  • 407
65
votes
21 answers

MVC 4 client side validation not working

Can anyone tell me why client side validation is not working in my MVC 4 application. _layout.schtml @Scripts.Render("~/bundles/jquery") @RenderSection("scripts", required: false) In my web.config I have:
Javid
  • 821
  • 2
  • 8
  • 13
64
votes
4 answers

jQuery Validation plugin - Validating hidden inputs and not visible?

How would I validate hidden inputs and not visible text inputs with jQuery Form Validation plugin? The problem is, that I'm using auto-suggest plugin, which generates a hidden input for selected items:
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66
62
votes
9 answers

When form is validated, how to SCROLL to the first error instead of jumping?

I've seen many questions with variations on this theme, but I'm looking for the straightforward solution: HTML form, jQuery validation, multiple fields are required. When the form is submitted, validation jumps to the first error and highlights it.…
Heraldmonkey
  • 2,111
  • 2
  • 19
  • 29
62
votes
10 answers

jQuery Validation using the class instead of the name value

I'd like to validate a form using the jquery validate plugin, but I'm unable to use the 'name' value within the html - as this is a field also used by the server app. Specifically, I need to limit the number of checkboxes checked from a group.…
Matt
  • 1,140
  • 2
  • 10
  • 17
62
votes
4 answers

Custom Error Label Placement using jQuery validate (For all or some of your errors)

I would like to place one error label (Not All) in a custom location. jQuery provides this http://docs.jquery.com/Plugins/Validation/validate#toptions option but I could not find anything about how to place one specific error message and not change…
chainwork
  • 2,890
  • 4
  • 30
  • 29
61
votes
7 answers

Error in jquery.validate.js in MVC 4 Project with jQuery 1.9

I created a new ASP.Net MVC 4 project using the template in Visual Studio 2012. After upgrading to jQuery 1.9, the login functionality breaks. Specifically, I get the error 0x800a138f - JavaScript runtime error: Unable to get property 'call' of…
Eric J.
  • 147,927
  • 63
  • 340
  • 553
60
votes
10 answers

Jquery validation plugin - TypeError: $(...).validate is not a function

My script throw errors: TypeError: jQuery.validator is undefined additional-methods.js:20 TypeError: $(...).validate is not a function index.php:115 Probably, I have mistake in jQuery code.