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 7184843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:17:05+00:00 2026-05-28T18:17:05+00:00

I have two select elements: hours and minutes. If one or both fields are

  • 0

I have two select elements: hours and minutes. If one or both fields are left blank I need an error to appear. I have been using the errorPlacement function to place just one error, but when you correct that one field, the error disappears but the other field is still invalid.

I have tried various combinations of validator.addMethod but there are always issues where both errors will show, or no errors will show etc.

Has anyone faced this issue before? Or does anyone know of a way to detect and handle the errors in the correct way?

Thanks in advance.

  • 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-28T18:17:07+00:00Added an answer on May 28, 2026 at 6:17 pm

    I’m not sure which field is required in your example, but you may be looking for dependency expression for jquery Validation. For example, if you wanted to make minutes required only if hours has a value, you would setup the validation like this:

    rules: {
        minutes: {
          required: "#hours:filled"
        }
    }
    

    If this is not your desired logic, you will most likely have to create a custom validation method. Is this the logic you had in mind?

    For reference:

    jQuery Validation Dependency Expression

    Edit:

    Here is a custom validator:

    jQuery.validator.addMethod("hoursMinutes", function (value, element, param) {
        return $("#hours").val() != "" && $("#minutes").val() != "";
    }, "Please enter both hours and minutes.");
    
    validator.settings.rules = {
        hours: { hoursMinutes: true }
    }
    

    This will make only 1 error message show up.

    Edit #2:

    Try this.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
    
                var validator = $("#form1").validate({
                    highlight: function (element, errorClass) {
                        $(element).addClass("invalid");
                    },
                    unhighlight: function (element, errorClass) {
                        $(element).removeClass("invalid");
                    },
                    groups: { time: "hours minutes" },
                    errorPlacement: function (error, element) {
                        $("#errors").append(error);
                    },
                    messages: {
                        hours: { required: "Please enter both hours and minutes." },
                        minutes: { required: "Please enter both hours and minutes." }
                    },
                    showErrors: function (errorMap, errorList) {
                        this.defaultShowErrors();
    
                        if ($("#hours:filled, #minutes:filled").length == 2) {
                            $("#errors label").hide();
                        }
                        else {
                            $("#errors label").show();
                        }
                    }
                });
    
                $("#submitBtn").click(function () {
                    if ($("#form1").valid()) {
                        alert("valid");
                    }
    
                    return false;
                });
            });
        </script>
        <style type="text/css">
            .invalid
            {
                border: solid 1px red;
            }
        </style>
    </head>
    <body>
        <form id="form1" action="" method="post">
        <div id="errors">
        </div>
        <input id="hours" name="hours" type="text" class="required" />
        <input id="minutes" name="minutes" type="text" class="required" />
        <button id="submitBtn">
            Go</button>
        </form>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two <select> elements. The first one contains countries (e.g. USA, Canada, England,
I have a select element. <select class='cSelectType'> <option value=1>one</option> <option value=2>two</option> <option value=3>three</option> <option
I have two html select boxes where items are moved from left to right,
I have two elements that I would like to select <input id=iMe /> and
I have two select with the same elements (options) in it. What I am
I want to select all the elements that have the two classes a and
How do I select elements that have two attributes? I can do @name |
I have two columns. People have to select elements from the first column and
I have two different elements with the same id . How do I select
I have a webpage with different elements (a list of links and two select

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.