Is there masked input plugin for knockout.js using extenders?

前端 未结 5 2000
孤独总比滥情好
孤独总比滥情好 2021-02-02 14:21

I\'ve seen this post - it shows one possible solution. But I would like to have a more elegant way of doing masked input.

It should also play nicely with knockout valida

5条回答
  •  深忆病人
    2021-02-02 14:53

    I tried to use the first answer but it did not work with ko.validation plug in. My validation errors were not being displayed.

    I wanted to have little bit more intuitive ko binder. Here is my solution. I am using jquery.inputmask plug in. I also wipe out the property on my viewmodel if not value entered.

        ko.bindingHandlers.mask = {
            init: function (element, valueAccessor, allBindingsAccessor, viewModel,     bindingContext) {
                var mask = valueAccessor() || {};
                $(element).inputmask({ "mask": mask, 'autoUnmask': false });
                ko.utils.registerEventHandler(element, 'focusout', function () {
                    var value = $(element).inputmask('unmaskedvalue');            
                    if (!value) {
                        viewModel[$(element).attr("id")]("");                
                    }
                });
            }
        };
    

    Here is the usage:

    
    

提交回复
热议问题