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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:23:58+00:00 2026-06-18T11:23:58+00:00

In a ViewModel I have a few properties which have required and regular expression

  • 0

In a ViewModel I have a few properties which have required and regular expression validations (on the same property) as follows:

// ...other properties...
MaxDays: ko.observable("").extend({
    required: { message: "You have to specify the maximum number of days." },
    pattern: {
        message: "Please enter a valid number.",
        params: '[0-9]+$',
        maxLength: 10
    }
}),
// ...other properties...

I’m using Jasmine to test and I noticed that if I assign a value that doesn’t match the expression, the value is ignored and it runs its own test for "required" as if there’s no data in that property.

// ... describe, other "it" statements, etc...
it("should complain if there's incorrect data", function () {

    viewModel.MaxDays("Zweiundzwanzig");

    expect(viewModel.errors().length).toBeGreaterThan(0);
    expect(viewModel.errors()).toContain('Please enter a valid number.');

    console.log(viewModel.errors());

});
// ...

When I run the test it fails. the errors collection is filled with an error but not because I entered a string when it should be a number. It fails because it thinks that property is empty. I get the error message from "required" instead of the message from "pattern":

enter image description here

The funny part is that it works on the UI, so if I go on the field bound to that property and type a string value, it will fire the regex validation and put the correct error message beside the field.

Can anyone point out what I’m missing here?

  • 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-06-18T11:24:00+00:00Added an answer on June 18, 2026 at 11:24 am

    knockout.validation.group.errors() does currently not update correctly if the error type changes (see https://github.com/ericmbarnard/Knockout-Validation/issues/218).

    I assume that this is your problem. Try to set ‘MaxDays’ to a correct value and then to the invalid one. That should fix the test if that was your problem.

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

Sidebar

Related Questions

In my ViewModel I have a property, which is a list of models: private
I have a ViewModel which exposes the string property PageToolBarVisible which can be true
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i
I have a viewmodel which is subscribing to the event NavigationCompletedEvent. This viewmodel need
I have one ViewModel for enums handling (few entities in Entity Framework). 1) Philosophically,
My ViewModel has a collection of model. I have few questions in this regard.
I have seen a few examples where the viewModel (in Silverlight apps) is in
I have a very large entity with a few hundred properties. I have a
I have a user control let say UC1 . This user control have viewmodel
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a

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.