The following snippet does what I want to an input
, i.e., it removes all non-alphanumerical characters, converts to uppercase, and preserves the cursor position.
element = $(element);
element.keyup(function() {
var x = element.val();
var y = x && x.toUpperCase().replace(/[^A-Zd]/g, '');
if (x===y) return;
var start = this.selectionStart;
var end = this.selectionEnd + y.length - x.length;
element.val(y);
this.setSelectionRange(start, end);
});
I placed this snippet in the link
of a directive and it works.... mostly.
The problem is that the angular
model sees the value before the change gets applied. I tried to Google for how to use $apply
or $digest
or whatever here, but nothing worked.
(Actually, I somehow managed it, but then the content was re-rendered and I lost the position. I can't reproduce it, but it wasn't good enough, anyway.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…