How to validate a form with multiple checkboxes to have atleast one checked
$(‘#subscribeForm’).validate( { rules: { list: { required: true, minlength: 1 } } }); I think this will make sure at least one is checked.
$(‘#subscribeForm’).validate( { rules: { list: { required: true, minlength: 1 } } }); I think this will make sure at least one is checked.
You can checkout the rules add function, but basically here’s what you can do: jQuery(function() { // You can specify some validation options here but not rules and messages jQuery(‘form’).validate(); // Add a custom class to your name mangled input and add rules like this jQuery(‘.username’).rules(‘add’, { required: true, messages: { required: ‘Some custom message … Read more
I’ve summarized here the correctly-working source code, which resulted from applying the accepted answer. Hope you find it useful. RequiredCheckbox.aspx <%@ Page Language=”C#” Inherits=”System.Web.Mvc.ViewPage<RegistrationViewModel>” %> <!DOCTYPE html> <html> <head runat=”server”> <title>RequiredCheckbox</title> <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js” type=”text/javascript”></script> <script src=”https://ajax.microsoft.com/ajax/jQuery.Validate/1.7/jQuery.Validate.js” type=”text/javascript”></script> <script src=”https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js” type=”text/javascript”></script> <script type=”text/javascript” language=”javascript”> $.validator.unobtrusive.adapters.addBool(“mandatory”, “required”); </script> </head> <body> <div> <% // These directives can occur … Read more
You should have ‘name’ attribute for your inputs. You need to add the rules dynamically, one option is to add them when the form submits. And here is my solution that I’ve tested and it works: <script type=”text/javascript”> $(document).ready(function() { var numberIncr = 1; // used to increment the name for the inputs function addInput() … Read more
I just ran into this issue myself. Pulled my hair out for an hour, but here it is: Throw the “name” attribute in there as well and that may do the trick.
Just add ignore: [], in Your form validation function. It will enable hidden field validation.So You will get validation for Select 2 $(“#ContactForm”).validate({ ignore: [], rules: { //Rules }, messages: { //messages } });
if you look at the “rules” section’s example code in the documentation page, there is a depends field you can set. something like the following (this is right off my head, not tested): … secondInput: { required: function(element){ return $(“#firstInput”).val()!=””; } } ….
The unobtrusive library will attach the validator on all forms, so you have to replace the submitHandler this way: $(“form”).data(“validator”).settings.submitHandler = function (form) { alert(‘submit’); form.submit(); };
Instead of changing the source file jquery.validation you can simply override the function you need to edit only in the pages that requires it. An example would be: $.validator.prototype.checkForm = function() { //overriden in a specific page this.prepareForm(); for (var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++) { if (this.findByName(elements[i].name).length !== undefined … Read more
What you should use is the errorLabelContainer jQuery(function($) { var validator = $(‘#form’).validate({ rules: { first: { required: true }, second: { required: true } }, messages: {}, errorElement : ‘div’, errorLabelContainer: ‘.errorTxt’ }); }); .errorTxt{ border: 1px solid red; min-height: 20px; } <script type=”text/javascript” src=”http://code.jquery.com/jquery-1.11.1.js”></script> <script type=”text/javascript” src=”http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js”></script> <script type=”text/javascript” src=”http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js”></script> <form id=”form” method=”post” … Read more