I have an HTML text input called hours that is verified as a number through a knockout extender. If the user entering the hours has any whitespace after the number they enter, I would like to simply have this trimmed without getting notified that what they've entered for hours is not a number. For example, if they enter "2.5 ", this should be correct as I want the extra spaces trimmed automatically. How am I able to do this with what I have below? Thank you.
hours: ko.observable().extend({ number: true, required: true })
You can add a trimming functionality to observables, e.g. adding a custom function to
ko.subscribable.fnas explained in another SO post:P.S. Don't be tempted to add a
trim()call into Knockout validator pluginnumberrule:... because you don't want the trimming to happen during validation, but much earlier, that is: during the writing.