views:

69

answers:

3

I can only retrieve the value without the newly pressed key. Using the keyup event isn't an option, because it does not fire if the user doesn't release the key. This is important because I want to act upon every single keypress.

Combining the old value with the keyCode that is reachable from the event's arguments isn't acceptable either, because it's not guaranteed that the user will type to the end of the string in the textbox.

+2  A: 

Wrap your handler code in setTimeout(function() { ... }, 0).

This will execute the code in the next message loop, after the value has been updated.

SLaks
+1 Confirmed, this works: http://jsfiddle.net/3tRMx/
Ender
But then I would lose the opportunity to prevent the event. However if I combine this with Jeff T's suggestion, I could rollback the input's value to its previous state if I find it invalid. I'm still open to other ideas though.
kahoon
If you want to be able to rollback to the previous state, you could save the value before entering the setTimeout, and refer back to it if you discover the need to rollback. Updated jsFiddle here: http://jsfiddle.net/3tRMx/1/ Try entering 'xx' to see the rollback at work.
Ender
@Ender: I modified your code and discovered an interesting issue: If you keep a key pressed continuously for long enough some keys eventually "escape", so they will appear regardless that they are supposed to be deleted in the next message loop. I guess this happens because the next keypress event happens before the previous keypress' setTimeout callback. Related example: http://jsfiddle.net/vrjAA/ (hold the letter "i" pressed for some time)
kahoon
@kahoon - Ha! Right you are. After a bit of thinking, I came up with something which I think solves the problem. Turn oldVal into a global (or at least semi-global) variable, and update it in the keydown event instead of the start of keypress. Using this, I held a key down for at least a minute with no "escapees." Modified your jsFiddle here: http://jsfiddle.net/vrjAA/1/
Ender
+1  A: 

Here's an idea that might work. Use keypress and store the val at that point so you can always compare the current value to the last value and find the difference in the strings. The difference will be the key that was pressed.

One way to do this would be to turn the strings into arrays and compare the 2 arrays like they are doing here: http://stackoverflow.com/questions/1187518/javascript-array-difference

Never tried anything like this so it might not be viable but might be worth a shot.

Jeff T
A: 

It's possible to work out what the value will be after the keypress. It's easy in non-IE browsers and trickier in IE, but the following will do it:

document.getElementById("your_input").onkeypress = function(evt) {
    var val = this.value;
    evt = evt || window.event;
    var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;
    if (charCode) {
        var keyChar = String.fromCharCode(charCode);
        var start, end;
        if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
            start = this.selectionStart;
            end = this.selectionEnd;
        } else if (document.selection && document.selection.createRange) {
            // For IE up to version 8
            var selectionRange = document.selection.createRange();
            var textInputRange = this.createTextRange();
            var precedingRange = this.createTextRange();
            var bookmark = selectionRange.getBookmark();
            textInputRange.moveToBookmark(bookmark);
            precedingRange.setEndPoint("EndToStart", textInputRange);
            start = precedingRange.text.length;
            end = start + selectionRange.text.length;
        }
        var newValue = val.slice(0, start) + keyChar + val.slice(end);
        alert(newValue);
    }
};
Tim Down
kahoon, does this help you?
Tim Down
Thank you, this is the closest to what I wanted to achieve. One major issue though: it still doesn't protect from copy and pasting arbitrary data into the input.
kahoon
OK. You didn't mention that in the question. In most browsers you can prevent that with a simple `document.getElementById("your_input").onpaste = function() { return false; };`. Firefox 2 and I think all version of Opera don't support the paste event.
Tim Down