Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:04:51+00:00 2026-05-30T13:04:51+00:00

The jQuery Validation plugin works great and is very easy to use: $(".selector").validate({ })

  • 0

The jQuery Validation plugin works great and is very easy to use:

$(".selector").validate({
})

Just by setting CSS classes like "required email", the default message will be displayed.

However, I need to customize the messages. The documentation says you can specify rules using a key-value pair for elements and their corresponding messages:

$(".selector").validate({
   rules: {
     name: "required",
     email: {
       required: true,
       email: true
     }
   },
   messages: {
     name: "Please specify your name",
     email: {
       required: "We need your email address to contact you",
       email: "Your email address must be in the format of name@example.com"
     }
   }
})

But, it is not practical to specify a rule for every form element, especially server-generated controls in ASP.NET. Is it possible to specify rules that would apply to ALL elements? Or can I use a class selector somehow?

I tried the following, but it didn’t work:

$("#frmMyForm").validate
({
    rules:
    {
        $(".required email"):
        {
            required: true,
            email: true
        }
    },
    messages:
    {
        $(".required email"):
        {
            required: "Please enter your email address",
            email: "Your email address must be in the format of name@example.com"
        }
    }
});

That seemed to have a syntax error – the plugin didn’t do anything. Then I tried:

$("#frmMyForm").validate
({
    rules:
    {
        ".required email":
        {
            required: true,
            email: true
        }
    },
    messages:
    {
        ".required email":
        {
            required: "Please enter your email address",
            email: "Your email address must be in the format of name@example.com"
        }
    }
});

This didn’t have any syntax error – the plugin worked, but it ignored the rules/custom messages. Has anyone here used jQuery Validation plugin? If so, how did you apply rules/custom messages to multiple elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:04:53+00:00Added an answer on May 30, 2026 at 1:04 pm

    For the purposes of my example, this is the base starting code:

    HTML:

    <input type="text" name="field_1" />
    <input type="text" name="field_2" />
    <input type="text" name="field_3" />
    

    JS:

    $('#myForm').validate({
        rules: {
            field_1: {
                required: true,
                number: true
            },
            field_2: {
                required: true,
                number: true
            },
            field_3: {
                required: true,
                number: true
            }
        }
    });
    

    DEMO: http://jsfiddle.net/rq5ra/


    NOTE: No matter which technique below is used to assign rules, it’s an absolute requirement of the plugin that every element has a unique name attribute.


    Option 1a) You can assign classes to your fields based on desired common rules and then assign those rules to the classes. You can also assign custom messages.

    HTML:

    <input type="text" name="field_1" class="num" />
    <input type="text" name="field_2" class="num" />
    <input type="text" name="field_3" class="num" />
    

    The .rules() method must be called after invoking .validate()

    JS:

    $('#myForm').validate({
        // your other plugin options
    });
    
    $('.num').each(function() {
        $(this).rules('add', {
            required: true,
            number: true,
            messages: {
                required:  "your custom message",
                number:  "your custom message"
            }
        });
    });
    

    DEMO: http://jsfiddle.net/rq5ra/1/

    Option 1b) Same as above, but instead of using a class, it matches a common part of the name attribute:

    $('[name*="field"]').each(function() {
        $(this).rules('add', {
            required: true,
            number: true,
            messages: { // optional custom messages
                required:  "your custom message",
                number:  "your custom message"
            }
        });
    });
    

    DEMO: http://jsfiddle.net/rq5ra/6/


    Option 2a) You can pull out the groups of rules and combine them into common variables.

    var ruleSet1 = {
            required: true,
            number: true
        };
    
    $('#myForm').validate({
        rules: {
            field_1: ruleSet1,
            field_2: ruleSet1,
            field_3: ruleSet1
        }
    });
    

    DEMO: http://jsfiddle.net/rq5ra/4/


    Option 2b) Related to 2a above but depending on your level of complexity, can separate out the rules that are common to certain groups and use .extend() to recombine them in an infinite numbers of ways.

    var ruleSet_default = {
            required: true,
            number: true
        };
    
    var ruleSet1 = {
            max: 99
        };
    $.extend(ruleSet1, ruleSet_default); // combines defaults into set 1
    
    var ruleSet2 = {
            min: 3
        };
    $.extend(ruleSet2, ruleSet_default); // combines defaults into set 2
    
    var ruleSet3 = { };
    $.extend(ruleSet3, ruleSet1, ruleSet2); // combines sets 2 & 1 into set 3.  Defaults are included since they were already combined into sets 1 & 2 previously.
    
    $('#myForm').validate({
        rules: {
            field_1: ruleSet2,
            field_2: ruleSet_default,
            field_3: ruleSet1,
            field_4: ruleSet3
        }
    });
    

    End Result:

    • field_1 will be a required number no less than 3.
    • field_2 will just be a required number.
    • field_3 will be a required number no greater than 99.
    • field_4 will be a required number between 3 and 99.

    DEMO: http://jsfiddle.net/rq5ra/5/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use jquery validate. Following the example: http://docs.jquery.com/Plugins/Validation works great and I'd
I use jquery validation plugin and the remote attribute works with emailId but not
I'm using jquery's popular Validate plugin to validate all my fields. This works great
I use jquerylive plugin to add additional css classes to .field-validation-error class like this:
I'm using Thickbox 3.1 with jQuery 1.3.2 with the Validation plugin and all works
I am using the Validation Plugin for jQuery and it works wonders. Except when
Using the jQuery Validation plugin and AJAX, how can I validate the contents of
I use jQuery Validation plugin to ensure what user entered positive cost_of_car in first
I use a jquery validation plugin and I need to add some extra checking,
I'm using the jQuery validation plugin in a very similar manner to the Remember

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.