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
2 answers

Custom Unobtrusive Validator Not Firing

I have a custom unobtrusive validator within my ASP.NET MVC project. I have allowed multiple instances of the attribute on a particular property. Everything appears to render correctly. each instance of the attribute contains another lower-case…
Jason N. Gaylord
  • 7,910
  • 15
  • 56
  • 95
2
votes
1 answer

Jquery Validation: Get rid of label tag for error

I'm having a hard time using jQuery Validation. In particular, I'm trying to remove the error message from the label tag and put it inside a div. I have 5 blocks of radio buttons. Each block looks like this:
TWLATL
  • 2,859
  • 4
  • 25
  • 37
2
votes
2 answers

How to validate Bootstrap modal before dismiss with jQuery validation

I have a form on a Bootstrap modal, which if submitted, it takes the data entered and appends it on to a form behind the modal... I need these fields to validate before the modal is dismissed. This what I have currently: Modal:
Renier
  • 1,523
  • 4
  • 32
  • 60
2
votes
1 answer

Using jquery-validate (or similar) and JSONSchema to validate forms

We have a web app containing several forms, the majority of them with a few dozen fields. Right now we create json objects with the data entered in the forms and send those objects to REST services which, in turn, persist that data into a NoSQL…
2
votes
2 answers

Form Validation Jquery in html

I am new to JQuery and I want to use validation in html form Here is my code but it is not working .I added jquery validate but it's not working. I am new to JQuery and I want to use validation in html form …
chandan111
  • 251
  • 2
  • 6
  • 16
2
votes
1 answer

jquery Validation on Multi Step Form

I have this multi-step form which I would like to validate (to make sure video file with correct format is specified and all the required field are filled up). I want to use jquery.validate plugin to achieve this. The problem is that the "Upload"…
SCC
  • 87
  • 1
  • 3
  • 9
2
votes
2 answers

Using jquery Validation 'greaterthan' function

I am using the jQuery Validation method "greaterThan" as described by Jon and Mike E. in Validate that end date is greater than start date with jQuery But when I send over: EndDate: { greaterThan: "#StartDate" } The result message is 'Must be…
SC_Adams
  • 156
  • 1
  • 1
  • 13
2
votes
1 answer

valid() function not working in jquery form validation MVC3

I am creating a simple mvc3 application. the VIew is @using…
2
votes
1 answer

TypeError: $.validator.methods[method] is undefined

I have some problem but I can't figure it out, I have see some examples and I have read few posts about this method, but still nothing. I get error: TypeError: $.validator.methods[method] is undefined And belove that is: result =…
2
votes
1 answer

Foolproof RequiredIfTrue not working for MVC5?

I am trying conditional required field. If user selects ContactByPhone checkbox I am showing ContactPhoneNumber field and it should be required filed. If user doesn't select ContactByPhone then ContactPhoneNumber is invisible and not required.…
James123
  • 11,184
  • 66
  • 189
  • 343
2
votes
0 answers

Testing with jasmine with angular and unobtrusive jquery validate

I am using angular js with jquery unobtrusive validation for mvc application . I am writing unit tests for my angular code and i have been using jasmine and sinon for mocking my services . Everything is working fine but i dont have any idea on how i…
gaurus
  • 426
  • 1
  • 4
  • 16
2
votes
2 answers

validation at least one checkbox (multiple group of checkboxes)

I know this've been answered in here But my html looks like below :
2
votes
1 answer

RemoteAttribute Validation passes the wrong value of AdditionalFields

I am using a RemoteAttribute for some custom validation in my form. For simplicity, I have a radio group, PrimarySupportingDocument, with 3 options. If it is a certain selection the remote validation should validate that two textbox values, VIN and…
Pandagrrl
  • 106
  • 2
  • 6
2
votes
4 answers

jquery validation plugin and check unique field?

I am currently using jQuery validation plugin with cakephp in my new project. It's working perfectly untill I need to make unique check to email field through ajax to check with the database.. I didn't know how to make the plugin make a unique…
assaqqaf
  • 1,575
  • 4
  • 21
  • 38
2
votes
2 answers

javascript regex evaluation causes browser to hang

I have a problem with Regex when used for custom validation. I have this unobtrusive method: jQuery.validator.addMethod("isRegex", function (value, element, params) { if (value.length < 1) return true; var re = new RegExp(params.regex); …
Nick
  • 2,877
  • 2
  • 33
  • 62