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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:15:23+00:00 2026-06-12T05:15:23+00:00

This seems to be a common approach to sanitizing/validating/formatting data with knockout when binding

  • 0

This seems to be a common approach to sanitizing/validating/formatting data with knockout when binding to an input field, it creates a reusable custom binding that uses a computed observable. It basically extends the default value binding to include an interceptor that will format/sanitize/validate input before written/read.

ko.bindingHandlers.amountValue = {
  init: function (element, valueAccessor, allBindingsAccessor) {
    var underlyingObservable = valueAccessor();

    var interceptor = ko.computed({
      read: function () {
        // this function does get called, but it's return value is not used as the value of the textbox.
        // the raw value from the underlyingObservable is still used, no dollar sign added. It seems like 
        // this read function is completely useless, and isn't used at all
        return "$" + underlyingObservable();
      },

      write: function (newValue) {
        var current = underlyingObservable(),
            valueToWrite = Math.round(parseFloat(newValue.replace("$", "")) * 100) / 100;

        if (valueToWrite !== current) {
          // for some reason, if a user enters 20.00000 for example, the value written to the observable
          // is 20, but the original value they entered (20.00000) is still shown in the text box.
          underlyingObservable(valueToWrite);
        } else {
          if (newValue !== current.toString())
            underlyingObservable.valueHasMutated();
        }
      }
    });

    ko.bindingHandlers.value.init(element, function () { return interceptor }, allBindingsAccessor);
  },

  update: ko.bindingHandlers.value.update
};

jsFiddle example: http://jsfiddle.net/6wxb5/1/

Am i missing something? I’ve seen this method used everywhere, but it doesn’t seem to fully work. The read function seems completely useless as it doesn’t get used at all.., and in the write function, entering “23.0000” changes the written value to 23, but the textbox values do not update.

  • 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-12T05:15:24+00:00Added an answer on June 12, 2026 at 5:15 am

    The issue comes from the update portion of your custom binding. This part will update the field based on the original model value. So, the event handler attached in the init will send the new value through your writeable computed, but the updating of the field actually happens in the update.

    One option is to apply the value binding from your init function and skip the update function like:

    ko.bindingHandlers.amountValue = {
      init: function (element, valueAccessor, allBindingsAccessor) {
        var underlyingObservable = valueAccessor();
    
        var interceptor = ko.computed({
          read: function () {
            // this function does get called, but it's return value is not used as the value of the textbox.
            // the raw value from the underlyingObservable, or the actual value the user entered is used instead, no   
            // dollar sign added. It seems like this read function is completely useless, and isn't used at all
            return "$" + underlyingObservable();
          },
    
          write: function (newValue) {
            var current = underlyingObservable(),
                valueToWrite = Math.round(parseFloat(newValue.replace("$", "")) * 100) / 100;
    
            if (valueToWrite !== current) {
              // for some reason, if a user enters 20.00000 for example, the value written to the observable
              // is 20, but the original value they entered (20.00000) is still shown in the text box.
              underlyingObservable(valueToWrite);
            } else {
              if (newValue !== current.toString())
                underlyingObservable.valueHasMutated();
            }
          }
        });
    
          ko.applyBindingsToNode(element, { value: interceptor });
      }
    };
    

    Updated fiddle: http://jsfiddle.net/rniemeyer/Sr8Ev/

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

Sidebar

Related Questions

This seems to be a common problem, but I cant seem to fin a
This seems like it would be a common issue to be but I don't
I've looked around for a solution but it seems this is not a common
This seems to be a fairly common thing to do, and I've managed to
This seems to be quite a common theme and a few people have given
I have a form which submits to an iFrame (this is a common approach
This seems like a pretty common problem, but I haven't found any sort of
This seems to be like a common use case... but somehow I cannot get
This may be something common and trivial, but I seem to be having trouble
This seems a 'stupid' question, but let me explain. I'm working for a company

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.