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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:48:39+00:00 2026-05-28T19:48:39+00:00

I am seeing a lot of help writing custom jQuery validation for any type

  • 0

I am seeing a lot of help writing custom jQuery validation for any type of field on my page, but how would I write a custom validation method that takes values from multiple fields into account (cross-filed validation)? I think I’m supposed to do $.validator.addMethod to add my custom method and then add a rule to the validator object (instead of to a specific field), but I am having trouble with the syntax. Can anyone help?

For example,

I have two fields: date and time. My form is valid only if:

  1. Both are filled
  2. The date is not a sunday
  3. The time is between 6am and 6pm
  • 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-28T19:48:40+00:00Added an answer on May 28, 2026 at 7:48 pm

    You do need some custom rules here, but each validation method doesn’t need to take multiple fields into account. Seems to me, you need three different rules: required applied to both fields (already built in) and then a custom rule for the time rule (6AM – 6PM) and the day rule (can’t be a Sunday):

    $.validator.addMethod("notsunday", function (value, element) {
        var date = Date.parse(value);
        return this.optional(element) || (date && date.getDay() !== 0);
    });
    
    $.validator.addMethod("businesshours", function (value, element) {
        var time = Date.parse(value);
        return this.optional(element) ||
            (time && (time.getHours() >= 6 && time.getHours() <= 18));
    });
    
    $(document).ready(function() {
        $("#test").validate({
            rules: {
                date: {
                    required: true,
                    notsunday: true
                },
                time: {
                    required: true,
                    businesshours: true
                }
            },
            messages: {
                date: {
                    notsunday: "Can't be a Sunday!"
                },
                time : {
                    businesshours: "Must be between 6AM and 6PM"
                }
            }
        });
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/7KFgn/

    Note that the example uses DateJS to make parsing the dates easier.

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

Sidebar

Related Questions

Please Help!! I'm seeing a lot of mixed results when I search for this,
I'm seeing a lot of of similar questions to this. but can't find one
I am debugging an web application that uses MySQL. I am seeing lot of
I've been seeing a lot of as-yet unexplainable now it works--now it doesn't issues
I'm seeing a lot of weird escaped characters typically in my web.config appearing.
I've been seeing a lot of C# 4.0 changes as of late. I really
I have been seeing this a lot lately and I am unable to find
I have a website where I cache a lot of info. I am seeing
Seeing lots of SOAP/RDS stuff, but just want to dump some MySQL data and/or
Seeing some strange things; help is being solicited. I have a query, like so:

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.