0

I've already asked this question in an earlier post but I am no nearer a resolution:

Knockout - Can't update Observable value using Subscribe (due to extension validation)

I have an observable (Amount) which is bound to a TextBoxFor.

@Html.TextBoxFor(m => m.Amount, new { type = "number", data_bind = "value: Amount" })

When a value is entered it needs to be validated to ensure it doesn't exceed 999999. In addition to this, if the value is less than 50 then it needs to be set to 50. These checks needs to be made each time the 'Amount' changes.

var viewModel = {
    Amount: ko.observable().extend({numeric: 0, max: 999999})
};

I've tried implementing the solutions (in the answer to my previous post) but I couldn't get these to work.

The best I have come up with is creating a computed as follows. This checks the input value and updates correctly on the first attempt. Subsequent value changes do not trigger a change on the screen but stepping through the code, do seem to update the ViewModel.Amount value.

@Html.TextBoxFor(m => m.Amount, new { type = "number", data_bind = "value: amountMin" })


quoteVehicleViewModel.VehicleMileage = ko.observable(0);


viewModel.amountMin = ko.computed({
    read: function () {
        return viewModel.Amount();  
    },
    write: function (value) {
        if (value < 50) {
            viewModel.Amount(50);
        }
    }
}).extend({ numeric: 0, max: 999999 });

** A "viewModel.Amount()" entered in the Console shows the value as being 1000 but the value on screen still shows as the value entered.

Any assistance gratefully received

John

Community
  • 1
  • 1
JohnT
  • 25
  • 1
  • 6
  • If you want to [get attention for your old question](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) it's also possible (even preferred) to edit the question and provide substantial updates (when you have more rep you can also opt to add a bounty to your question). – Jeroen Apr 22 '13 at 11:46
  • OK, thanks Jeroen. I'm new to posting on Stack Overflow so still finding the best way of doing things. – JohnT Apr 22 '13 at 12:57
  • That's okay, and welcome to SO! Just hit the [edit](http://stackoverflow.com/posts/16133318/edit) button on your other question and update it with new info. The question will be bumped to the front page if your edit's substantial. Good luck! – Jeroen Apr 22 '13 at 13:07

0 Answers0