Click here to Skip to main content
13,626,043 members
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 1 Jun 2014
Licenced CPOL

Trimmed Value Binding in knockout.js

, 12 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Trimmed value binding in knockout.js


In web applications, when we are working with form inputs; sometimes we have to trim the user input values to avoid unnecessary white spaces. So every input value needs to be trimmed and use of so many $.trim() comes ahead.


As the number of input grows, so does the use of $.trim(). And try to imagine doing the same thing in every form. To avoid such massive use of $.trim(), we can use custom knockout value binding handler which will ensure that if the user inputs/server send the value with unnecessary white spaces, it would be trimmed and will be reassigned to the input field.

Custom Binding Handler

Here is the custom binding handler which will help use in trimmed value binding:

/*trimmed value binder for knockout*/
ko.bindingHandlers.trimedValue = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        $(element).on("change", function () {
            var observable = valueAccessor();
            var trimedValue = $.trim($(this).val());
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        var trimedValue = $.trim(value);

Using The Handler In HTML

We are going to use the handler like this. It's really important to remember to use data-bind="trimedValue: " rather than data-bind="value: "

<h2>Trimed value building</h2>
    <span>change the values to see if triming is taking place or not</span>
    <!--First Name-->       
        <label>First Name</label>
        <input type="text" data-bind="trimedValue: firstName" />
    <!--Last Name-->       
        <label>Last Name</label>
        <input type="text" data-bind="trimedValue: lastName" />
        <input type="text" data-bind="trimedValue: age" />

View Model

Now let's see the view model:

/*View model*/
function ViewModel() {
    var self = this;
    self.firstName = ko.observable('');
    self.lastName = ko.observable('');
    self.age = ko.observable(0);
    /*initialize vm*/
    self.init = function () {
        self.firstName('   Dipon   ');      // only 'Dipon' would be bind to model
        self.lastName('   Roy    ');        // only 'Roy' would be bind to model
        self.age('  24 ');                  // only '24' would be bind to model

Finally, apply view model binding:

$(document).ready(function () {   
    //model binding with validation
    var vm = new ViewModel();

Find the JsFiddel example at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bangladesh Bangladesh
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt's not trim when you press "space" key ! Pin
Member 1111914929-Sep-14 16:49
memberMember 1111914929-Sep-14 16:49 
AnswerRe: It's not trim when you press "space" key ! Pin
diponsust29-Sep-14 19:50
memberdiponsust29-Sep-14 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180712.1 | Last Updated 12 Jun 2014
Article Copyright 2014 by DiponRoy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid