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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:25:18+00:00 2026-06-18T02:25:18+00:00

I have following attributes in my viewmodel which use knockout validation,one of them is

  • 0

I have following attributes in my viewmodel which use knockout validation,one of them is custom validation to check password match.

model.Password = ko.observable()
                  .extend({ required: { message: "Password is required.", 
                        params: true,
                        onlyIf: function () { return model.IsCredentialsRequired(); }}
                    });


model.ConfirmPassword = ko.observable().
                        extend({ validation: { validator: mustEqual,
                        message: 'Passwords do not match.',
                        params: model.Password,               
                        onlyIf: function () { return model.IsCredentialsRequired(); } }
                    });

code for custom validation function

var mustEqual = function (val, other) {
                return val == other();
            };

I found that OnlyIf condition is working fine for model.Password depending on model.IsCredentialsRequired() but its not working for model.ConfirmPassword,Can somebody help me why this is happening?Is there any another way by which i can use conditional validation for custom rules?

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-06-18T02:25:19+00:00Added an answer on June 18, 2026 at 2:25 am

    knockout-validation does currently not support ‘onlyIf’ for anonymous rules. But it is supported for custom rules. Therefore you can create a custom rule and use onlyIf with that:

    ko.validation.rules['confirmPasswordMatches'] = {
        validator: function (val, params) {
            var otherValue = params;
            return val === ko.validation.utils.getValue(otherValue);
        },
        message: 'Passwords do not match.',
    };
    ko.validation.registerExtenders();
    
    function ViewModel() {
        var self = this;
    
        self.IsCredentialsRequired = ko.observable(true);
    
        self.Password = ko.observable()
            .extend({
                required: {
                    message: "Password is required.",
                    params: true,
                    onlyIf: self.IsCredentialsRequired
                }
        });
    
        self.ConfirmPassword = ko.observable().
        extend({
                confirmPasswordMatches: {
                params: self.Password,
                onlyIf: self.IsCredentialsRequired
            }
        });
    
    }
    

    Fiddle: http://jsfiddle.net/delixfe/mFAEx/

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

Sidebar

Related Questions

Lets suppose I have a basic CustomerEntity which has the following attributes Name Surname
I created a Micropost model that have the following attributes: <Micropost id: 1, content:
I have the following view model which contains an array of elements function ReservationsViewModel()
I have a fragment shader with the following attributes: varying highp vec2 coordinate; precision
I have the following tables in a database (i'll only list the important attributes):
I have a following code , I want to display all the attributes of
I have the following XML array: [link]=> array(2) { [0]=> object(SimpleXMLElement)#311 (1) { [@attributes]=>
So I have the following XML chain: $xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue XML example In Some XML-files $xml->assessment->outcomes_processing->outcomes->...
In trying to select elements that have any attributes, the following throws a jQuery
I have following source. In insertMessage(..), it calls selectMessage to check whether duplicate record

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.