How to place cursor at the end of an input field with jQuery validation?
I am trying to place the cursor at the end of the input field after validation. This works fine, except if you type too many letters (more than the default length of the input box), the last letters do not show, because the value of the input is rewritten after th开发者_如何学JAVAe sanitization.
How can I modify this code to put the cursor at the end of the input field?
In the html:
<input type="text" id="myInput">
In the jQuery:
$("#myInput").keyup(
function(){
this.value = this.value.replace(/[^a-z]/gi,''); // only letters
}
);
Instead of validating in the keyup
event, try using the keydown
event as the character is not yet added to the input field when the event is fired.
You can do something like this...
$elem.keydown(function (e) {
if (isDisallowed(e.which)) {
e.preventDefault();
$('#errorMsg').text('That key is not allowed').delay(2000).fadeOut();
}
});
精彩评论