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
2
votes
1 answer

JQuery remote validation (doesn't block form submit)

I am facing the following problem. In the signup form it tries to remotly check, if a given username is already used. This works well and if a username is already in use, my adding an error class works fine. The problem is, that you can still submit…
user31336
  • 21
  • 2
2
votes
1 answer

jQuery turn off form validation

Im using the bog standard form validation plugin. I have two radio buttons 'yes' 'no' and what im trying to do is set validation if a radio 'yes' is checked... Which works, but i need to then turn off validation if the 'no' radio is selected. Im…
Dooie
  • 1,649
  • 7
  • 30
  • 47
2
votes
1 answer

How to customize jquery validation number format?

I want to set jQuery validation to consider as valid decimal point and not comma (0.5 <- not valid, 0,5 <- valid). How can I achieve this? Edit: I use ASP.NET MVC 4 form: I have some field with float type in model. I have for these fields Html…
dvjanm
  • 2,351
  • 1
  • 28
  • 42
2
votes
2 answers

jQuery Validate custom success message repeats on recheck

So I have this jQuery Validation plugin, and it has a custom success message. The form validates automatically, because it's attached to the body. The problem comes when I wish to check the variable to see if the form is valid. That revalidates it,…
Kyle Hotchkiss
  • 10,754
  • 20
  • 56
  • 82
2
votes
4 answers

Stop jQuery validate showing errorClass on success

I am looking to get hide the span.help-block on success because it is getting added(with no content) and pushing the rest of the content down I have tried adding into the success: function $('.help-block').hide() but this will only hide the span…
Pierce McGeough
  • 3,016
  • 8
  • 43
  • 65
2
votes
1 answer

How to get jquery validation status with knockout custom binding?

I try to create view model that has no view information, such as calling jquery validation directly from view model before saving / sending the data to server. var vm = function () { var self = this; self.password = ko.observable(); …
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
2
votes
1 answer

jquery-validate: validate if the sum of two fields is greater than X

I am using jquery-validate and I am trying to validate that the value of two fields (participants_adults and participats_child) is greater than X (a number). As an additional challenge, the second field (participats_child) may or may not exist, so…
Migsy
  • 43
  • 2
  • 5
2
votes
4 answers

Unable to validate jQuery multiselect dropdown with id

I am using jQuery multi-select drop down box. Because of it's default property I am unable to validate it with it's name attribute. So that I would like to validate with id. I am using this as reference. But my idea is not working. I don't know…
Sam
  • 5,040
  • 12
  • 43
  • 95
2
votes
2 answers

jQuery validation plugin addMethod firing incorrectly

I must be missing something obvious, but everything that I've tried for this is leaving me empty handed, so I'm a bit puzzled. I'm attempting to use the jQuery validation plugin with custom validation methods, but it seems to be hit or miss. It…
LoganEtherton
  • 495
  • 4
  • 12
2
votes
1 answer

Jquery Validation with errorContainer

I'm using the Jquery Validation script found at http://jqueryvalidation.org/ and am wanting it setup so when you click a button (not a submit button!) it checks the form for validation. If there is an error, it displays the error next to the fields…
BlueCaret
  • 4,719
  • 7
  • 30
  • 48
2
votes
2 answers

How do I validate a form without submitting and go to other page with jquery mobile and Jquery validation?

So I'm trying to validate a JQM form that has 4 parts and are divided in different pages so after I validate the first part I want to go to the second, if the form it's valid. I'm trying to validate with the JQuery.validate plugin but it doesn't…
falgranado
  • 97
  • 1
  • 11
2
votes
2 answers

Validation plugin is not validating all fields

I am using the jQuery validate plugin for doing form validation. it's just checking first field only. if first field validation is true then it's submitting form. it's not validating rest fields.. i am using bootstrap 3.0
404 Not Found
  • 1,223
  • 2
  • 22
  • 31
2
votes
1 answer

jQuery validation plugin multiple forms

I'm trying to add some functionality to a series of forms to validate two checkboxes. I'm using the jQuery validation plugin and for the life of me can't get it working. In fiddle I just get a 403 forbidden message when I submit it. On my actual…
KingFu
  • 1,358
  • 5
  • 22
  • 42
2
votes
2 answers

MVC4: On validation error, change the textbox color to red

I have a form which has some text boxes, and when the textbox doesn't contain valid value I get the error message but the textbox color doesn't change to red. This is what I have Model public class AddCompany { public int Id { get; set; } …
2
votes
2 answers

Format date dd.mm.yyyy for input text with jQuery Validation addMethod

I am having real trouble with this addMethod for validating a date from an input. It doesn't test the regex properly and i think it may be written with errors. The date should be in this format: dd.mm.yyyy . Please help... $(function() { …
Razvan N
  • 554
  • 3
  • 9
  • 29