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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:36:30+00:00 2026-06-13T07:36:30+00:00

I am trying to use knockout.validation plugin. I want created a custom validation rule

  • 0

I am trying to use knockout.validation plugin. I want created a custom validation rule for checking the duplicate array item while making entry in the array. I tried this :

ko.validation.rules['duplicate'] = {
    validator: function (val1, val2) {

    },
    message: 'The value is already exist'
};

ko.validation.registerExtenders();

Here val1 is the array and val2 is the parameter pass in with the extend method. The problem is, i don`t know how to access the current entered item in the array from custom validation function. help me please ?

  • 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-13T07:36:31+00:00Added an answer on June 13, 2026 at 7:36 am

    It would work better if you flip around the problem. Add the validation onto the text observable (or an observable object, if multiple fields are involved) that you are trying to add. Then when you trigger the add, check if the observable is valid. If it’s valid then add it to the array.

    http://jsfiddle.net/jearles/XY9XH/

    —

    <span>New Item: <input data-bind="value:newItem" /> <button data-bind="click: addItem">Add</button></span>
    <div data-bind="foreach: items">
        <span data-bind="text: $data"></span>
    </div>​
    

    —

    ko.validation.rules['duplicate'] = {
        validator: function (item, array) {
            return array.indexOf(item) == -1;
        },
        message: 'Value already exists!'
    };
    ko.validation.registerExtenders();
    
    var ViewModel = function() {
        var self = this;
        self.items = ko.observableArray();
        self.newItem = ko.observable().extend({duplicate: self.items});
        self.addItem = function() {
            if (self.newItem().length > 0 && self.newItem.isValid()) {
               self.items.push(self.newItem());
               self.newItem(null);
            }
        };
    }
    
    ko.applyBindings(new ViewModel());    
    

    ​

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

Sidebar

Related Questions

I am trying to use knockout.validation plugin. I created an exampleViewModel : function exampleViewModel()
I am trying to use knockout validation lib to validate an array of objects.
I'm trying to create custom binding in knockout to use it as multiple selection
I'm trying to use the Knockout Concurrency plugin in my project, and I'm currently
Trying to use Powershell to script the removal of specific custom errors from an
I am trying to use Knockout in the following scenario: I have jquery UI
Trying to use the new cleaner event handling in Knockout 2 along with the
I am trying use a javascript function while passing php variables in it. For
I am trying use a javascript function while passing php variables in it. For
I'm trying to use knockout to do a pretty basic binding but am having

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.