0

I have a form which I am calling in popup modal using cTools, I added some validation rules in custom JavaScript file but those validations are not working and form is getting submitted, though validation on drupal level is working like suppose if I make the field required in form by adding #required it works fine but adding rule for required in jquery is not working. Can you explain why and provide a solution for this ?

$('#form_ID').validate({
                    //errorElement: 'span',
                    rules:{
                        first_name: {
                            required: true,
                            minlength: 3,
                            maxlength:30
                        },
                        last_name: {
                            required: true,
                            minlength: 3,
                            maxlength:30
                        },
                        select: {
                            required: true
                        },
                    },
                    messages:{
                        first_name: {
                            required: "Please enter a first name",
                            minlength: "Your name must consist of at least 3 characters",
                            maxlength: "Your name must consist of at least 30 characters"
                        },
                        last_name: {
                            required: "Please enter a last name",
                            minlength: "Your last name must consist of at least 3 characters",
                            maxlength: "Your last name must consist of at least 30 characters"
                        },
                        select: {
                            required: "Please confirm you have approval from your authorised account representative to use this account.",
                        },
                    }

                });

These validations are not working on js level.

  • For obvious reasons, we can’t guess why your custom js code doesn’t work, no. Please edit the question and provide a full description of the problem, including the relevant portions of code. If it’s just a plain JS/jQuery problem Stack Overflow is the right place to ask. If you identify a Drupal-specific problem please flag for reopening once you’ve made the edits. Many thanks – Clive Mar 22 '18 at 8:44
  • @Clive Do you have any working example of adding validation on js level in cTools modal form and form should not submit until all validations are true ? – Mohit Wadhwa Mar 22 '18 at 8:46
  • I don’t, but we don’t provide or locate examples/tutorials here, just answer specific questions that are unique to Drupal. Your question may well be that, but we won’t know until you describe what the problem is in detail – Clive Mar 22 '18 at 8:47
  • Added code to explain what is not working. – Mohit Wadhwa Mar 22 '18 at 9:22
  • Thanks, the edit clears up that this is a plain JS/jQuery issue (Drupal doesn’t use the jQuery validate plugin), so please use Stack Overflow for this one – Clive Mar 22 '18 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.