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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:33:21+00:00 2026-06-10T23:33:21+00:00

I’m using knockout and the mapping plugin to automatically create my view model. I

  • 0

I’m using knockout and the mapping plugin to automatically create my view model. I have a bunch of amounts in my view model that I bind to textboxes. When a user changes an amount in a textbox, all I want is to make sure what they entered is a number, and that it’s greater than 0, and if it isn’t I want to replace what they entered with 0. This seems like it should be SO simple… with a custom binding or subscribe function.

Everything I’m reading about knockout validation talks about extenders and read/write computed observables, or adding yet another plugin (such as jquery validation). They all seem like overkill for the situation, and the extenders/computed observables have to be explicitly declared for each observable you want to validate. I have a lot of amounts that are automatically created using the mapping plugin, so this doesn’t seem reasonable.

Any help would be greatly appreciated!

  • 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-10T23:33:23+00:00Added an answer on June 10, 2026 at 11:33 pm

    For your specific scenario, one way to handle this is by creating a custom binding that is able to intercept the value and do your validation. This can be done by creating a writeable computed in the custom binding to bind against. The advantage is that you don’t have to worry about the mapping plugin customizing your object creation.

    It might look something like:

    ko.bindingHandlers.positiveNumericValue = {
        init : function(element, valueAccessor, allBindingsAccessor) {
            var underlyingObservable = valueAccessor();
            var interceptor = ko.computed({
                read: underlyingObservable,
                write: function(newValue) {
                    var current = underlyingObservable(),
                        valueToWrite = isNaN(newValue) ? 0 : parseFloat(+newValue);
    
                    if (valueToWrite < 0) {
                       valueToWrite = 0;   
                    }
    
                    //only write if it changed
                    if (valueToWrite !== current) {
                        underlyingObservable(valueToWrite);
                    } else {
                        //if the rounded value is the same as it was, but a different value was written, force a notification so the current field is updated to the rounded value
                        if (newValue !== current) {
                            underlyingObservable.valueHasMutated();
                        }
                    }   
                } 
            });
            ko.bindingHandlers.value.init(element, function() { return interceptor }, allBindingsAccessor);
        },  
        update : ko.bindingHandlers.value.update
    };
    

    Here is a sample: http://jsfiddle.net/rniemeyer/2TnSM/

    Another way would be to extend observables with an option to create the writeable computed.

    For your scenario, it might look like:

    ko.observable.fn.forcePositive = function() {
        var underlyingObservable = this;
        if (!this.forcePositiveInterceptor) {
             this.forcePositiveInterceptor = ko.computed({
                read: this,
                write: function(newValue) {
                    var current = underlyingObservable(),
                        valueToWrite = isNaN(newValue) ? 0 : parseFloat(+newValue);
    
                    if (valueToWrite < 0) {
                       valueToWrite = 0;   
                    }
    
                    //only write if it changed
                    if (valueToWrite !== current) {
                        underlyingObservable(valueToWrite);
                    } else {
                        //if the rounded value is the same as it was, but a different value was written, force a notification so the current field is updated to the rounded value
                        if (newValue !== current) {
                            underlyingObservable.valueHasMutated();
                        }
                    }   
                } 
            });
        }            
    
        return this.forcePositiveInterceptor;        
    };
    

    You would then bind against it like:

    <input type="text" name="age" data-bind="value: age.forcePositive()" />
    

    The way that I implemented it here, you would need to call is as a function forcePositive() so the writeable is initialized. This is so that you can just use the mapping plugin without any customization and just do this on any observable that you want to use this functionality with.

    Sample: http://jsfiddle.net/rniemeyer/Dy4MH/

    I think that either choice would work for your scenario. I probably favor the second choice, so that you could add more of these while using the normal bindings.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

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.