Created
August 19, 2010 10:48
-
-
Save andrewdavey/537593 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers.keyFilter = { | |
init: function(element, value, allBindings) { | |
var filter = ko.bindingHandlers.keyFilter[allBindings.keyFilter]; | |
if (!filter) return; | |
$(element).keypress(function(event) { | |
if (!filter(event.which)) { | |
event.stopPropagation(); | |
return false; | |
} | |
}); | |
}, | |
isDigit: function(keycode) { | |
var zero = 48; | |
var nine = 57; | |
return zero <= keycode && keycode <= nine; | |
} | |
}; | |
// usage | |
// <input type="text" data-bind="value: someIntegerValue, keyFilter: 'isDigit'"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment