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
397
votes
14 answers

jQuery validation: change default error message

Is there a simple way to change the default error values in the jQuery validation plugin? I just want to rewrite the error messages to be more personal to my app--I have a lot of fields, so I don't want to set the message individually for field…
Kevin Brown
  • 12,602
  • 34
  • 95
  • 155
383
votes
8 answers

jQuery Validate Plugin - How to create a simple custom rule?

How do you create a simple, custom rule using the jQuery Validate plugin (using addMethod) that doesn't use a regex? For example, what function would create a rule that validates only if at least one of a group of checkboxes is checked?
Edward
  • 4,331
  • 4
  • 20
  • 6
254
votes
13 answers

jQuery validate: How to add a rule for regular expression validation?

I am using the jQuery validation plugin. Great stuff! I want to migrate my existing ASP.NET solution to use jQuery instead of the ASP.NET validators. I am missing a replacement for the regular expression validator. I want to be able to do something…
PeterFromCologne
  • 10,213
  • 9
  • 36
  • 46
204
votes
9 answers

jQuery Validate - Enable validation for hidden fields

In the new version of jQuery validation plugin 1.9 by default validation of hidden fields ignored. I'm using CKEditor for textarea input field and it hides the field and replace it with iframe. The field is there, but validation disabled for hidden…
Igor
  • 3,576
  • 3
  • 23
  • 18
187
votes
30 answers

How to clear jQuery validation error messages?

I am using the jQuery validation plugin for client side validation. Function editUser() is called on click of 'Edit User' button, which displays error messages. But I want to clear error messages on my form, when I click on 'Clear' button, that…
Vicky
  • 9,515
  • 16
  • 71
  • 88
172
votes
12 answers

jQuery Validation plugin: disable validation for specified submit buttons

I have a form with multiple fields that I'm validating (some with methods added for custom validation) with Jörn Zaeffere's excellent jQuery Validation plugin. How do you circumvent validation with specified submit controls (in other words, fire…
Ted
  • 7,122
  • 9
  • 50
  • 76
162
votes
11 answers

jQuery Validate Required Select

I am trying to validate html select element using jQuery Validate plugin. I set "required" rule to true but it always passes validation because zero index is chosed by default. Is there any way to define empty value that is used by required…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
158
votes
15 answers

Bootstrap with jQuery Validation Plugin

I am trying to add validation to my form with jQuery Validation Plugin, but I'm having a problem where the plugin puts the error messages when I'm using input groups. $('form').validate({ rules: { firstname: { minlength: 3, …
Miguel Borges
  • 7,549
  • 8
  • 39
  • 57
158
votes
9 answers

How to manually trigger validation with jQuery validate?

I want to manually trigger validation including showing error messages with jQuery Validate. The scenario I am trying to accomplish is a form like this:
usr
  • 168,620
  • 35
  • 240
  • 369
118
votes
16 answers

Validate that end date is greater than start date with jQuery

How do I check/validate in jQuery whether end date [textbox] is greater than start date [textbox]?
input
  • 7,503
  • 25
  • 93
  • 150
116
votes
8 answers

how to check if a form is valid programmatically using jQuery Validation Plugin

I have a form with a couple of buttons and I'm using jQuery Validation Plugin from http://jquery.bassistance.de/validate/. I just want to know if there is any way I can check if the form is considered in valid state by jquery validation plugin from…
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
103
votes
13 answers

jquery.validate.unobtrusive not working with dynamic injected elements

I am working with ASP.Net MVC3, the easier way to use the client validation would be enabling the jquery.validate.unobtrusive. Everything works fine, for stuff that's right from server. But when I try to inject some new 'inputs' with javascript,…
xandy
  • 27,357
  • 8
  • 59
  • 64
98
votes
9 answers

jQuery Validate - require at least one field in a group to be filled

I'm using the excellent jQuery Validate Plugin to validate some forms. On one form, I need to ensure that the user fills in at least one of a group of fields. I think I've got a pretty good solution, and wanted to share it. Please suggest any…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
97
votes
8 answers

jQuery Validate Plugin - Trigger validation of single field

I've got a form that can optionally be pre-populated via facebook connect. Once a user connects, their name and email are automatically filled in. The problem is that this doesn't trigger the remote validation to check if the email already exists.…
ryan
  • 971
  • 1
  • 6
  • 3
82
votes
6 answers

Confirm Password with jQuery Validate

I'm trying to use jQuery validate plugin to confirm my password entry. However, I don't want it to be a required field. Just IF a user wants to change the password, they would need to confirm it. If not, both fields shouldn't be…
ldrocks
  • 1,095
  • 1
  • 10
  • 14
1
2 3
99 100