Is it possible to execute jQuery functions on chained/multiple DOM ID's at the same time?

StackOverflow https://stackoverflow.com/questions/21195861

  •  29-09-2022
  •  | 
  •  

Domanda

On a page, I have several modals that all have a required email address.

The basic JS validation is as follows:

$("#resendValidation-form").validate({
    // Rules for form validation
    rules: {
        email: {
            required: true,
            email: true
        }
    },

    // Messages for form validation
    messages: {
        email: {
            required: 'Please enter your email address',
            email: 'Please enter a VALID email address'
        }
    },

    errorPlacement: function (error, element) {
        error.insertAfter(element.parent());
    }
});

This validation is also required for #loginEmailAddress and #resendEmailAddress. Can I chain the ID's together somehow without having to have three instances of this code?

-- Update --

I am using the form in the JS, where a form is defined as follows in a Razor view:

@using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post, new { Email = Model, id = "forgotPassword-form" }))
È stato utile?

Soluzione

You should be able to just do it like CSS selectors

$("#resendValidation-form, #loginEmailAddress, #resendEmailAddress").validate({});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top