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

  • Home
  • SEARCH
  • 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 9128051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:25:20+00:00 2026-06-17T07:25:20+00:00

I am new to knockout, and trying to workout how to use validations. We

  • 0

I am new to knockout, and trying to workout how to use validations. We have the following piece of code –

    var MyObjectModel = function(myObject){
    var self = this;
    self.myNumber = ko.observable(myObject.number).trimmed();
    self.myNumber.extend({
        minLength: {
            params: 7,
            message: "My Number is too short."
        },
        maxLength: {
            params: 7,
            message: "My Number is too long."
        }
    });

    self.errors = ko.validation.group(self);
}

Now I would like to change the code so that either the length of myObject.number should be either exactly 7, or the field should be empty. How do I go about making this change ?

Any help would be appreciated. In case you think something is not clear, please let me know so I can elaborate more.

  • 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-17T07:25:21+00:00Added an answer on June 17, 2026 at 7:25 am

    You can use a custom pattern to it:

    var MyObjectModel = function(myObject){
        var self = this;
        self.myNumber = ko.observable(myObject.number).trimmed();
        self.myNumber.extend({
            pattern: {
                params: '^[0-9]{7}$|^$',
                message: "My Number must have 7 chars or stay empty."
            }
        });
    
        self.errors = ko.validation.group(self);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following knockout.js code I’ve got the following: var itemLine = function()
I am trying to use Knockout in the following scenario: I have jquery UI
I'm new to knockout.js and am trying to bind to the following object representing
Basically I'm very new to Knockout. I have been trying tons and tons of
Trying to use the new cleaner event handling in Knockout 2 along with the
I am new to Knockout.js, I have created an observable array and initialized with
I'm a new user to knockout.js and so far have been very impressed with
New programmer here, I am trying to understand and break down this code below
I'm fairly new to Knockout.js so I may simply be missing something. I'm trying
Been getting into Knockout and and slowly getting used to it. Trying to use

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.