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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:35+00:00 2026-05-27T08:22:35+00:00

I created a simple custom validation to make sure time entered is in a

  • 0

I created a simple custom validation to make sure time entered is in a 12 hour format and the text input has a ’99:99′ mask set, so I will get:

12:00

The validation works great on the server:

     private ValidationResult ValidateTwelveHourFormat(string time, ValidationContext validationContext)
    {
        if (String.IsNullOrWhiteSpace(time.Substring(0, 1))
            || Convert.ToInt32(time.Substring(0, 1)) > 1
            || Convert.ToInt32(time.Substring(1, 1)) > 2
            || Convert.ToInt32(time.Substring(3, 1)) > 5)
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }
        return ValidationResult.Success;
    }

However, the same validation on the client does not behave the way I would expect:

$.validator.unobtrusive.adapters.addSingleVal('timeformat', 'format');
$.validator.addMethod('timeformat', function (value, element, timeformat) {
if (value) {
    if (timeformat == "TwelveHour") {
        if (value.toString().substring(0, 1) == ""
                || value.toString().substring(0, 1) > 1
                || value.toString().substring(1, 1) > 2
                || value.toString().substring(3, 1) > 5) {
            return false;
        }
    }
}
return true;
});

The client will correctly validate the first digit only. So if I entered a 3, it would trigger fine, but not for any other positions in the string. I threw some alerts in there and it seems to trigger the method when I enter a character into the text. This behavior confuses me because I didn’t think this would fire before the form was first submitted, only after. Otherwise, the user would see error messages as they enter the form the first time and I don’t believe that jives with the way validation typically works.

1.) Why isn’t the client-side validating other character positions correctly?
2.) Why is the client validating when the form has yet to be submitted?

Update

1.) I used JavaScript’s substr() as it acts like C#’s String.Substring().

For question 2 – I’m using Data Annoations with the mvc framework. Let’s say I have a field set to be required. When I first enter that form, that field does not trigger validation on blur. I can enter crap, remove crap, but not trigger validation. That’s why I’m confused. It seems to differ from the default behavior. I would think validation would be triggered after a submission attempt is attempted like the example I mentioned.

  • 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-27T08:22:36+00:00Added an answer on May 27, 2026 at 8:22 am

    Question 1:

    Javascript String.substring() works differently than C#. The second parameter is not the length, but the ending index.
    http://www.w3schools.com/jsref/jsref_substring.asp

    Question 2:

    It usually validates onBlur too, in my experience.

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

Sidebar

Related Questions

I created a simple custom attribute on the sales/order entity. Now, for new orders,
I have created a simple Asp.Net custom control which automatically combines all the correct
I have created a custom validation rule (uniqueUserName). The problem I'm having is that
I created a simple databse using Oracle that has several tables and a few
I've created a simple module for displaying a flash game in a custom block
I have created a simple custom installer as described here . My new installer
I'm trying to create a simple custom validator for my project, and I can't
I'm trying to create an ultra simple paypal custom integration with rails. I'm following
Could someone help me on this, I have created simple web services using axis2
How to manually create Friendly URLs? (PHP) So I have created simple php file

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.