1

I have a function to convert uppercases to lowercases aslso to replace white spaces for small dashes.

The problem is, when a user for example makes a typo and wants to rectify it, if he tries to move the cursor back using the keyboard, he won't be able to do so as the cursor will always be on the last position of the string.

Here I have the JS fiddle with the example working: http://jsfiddle.net/R8N8F/7/

In the example I am using jQuery because I couldn't make it run with Javascript of jsfiddle, but this is the current function I am using:

function replaze(obj){
    obj.value = obj.value.toLowerCase().replace(/ /g, '-');
}

And this my HTML:

<input name="demo" autofocus="autofocus" onkeyup="replaze(this);" type="text" id="UserUsername">
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alvaro
  • 40,778
  • 30
  • 164
  • 336

2 Answers2

1

You can preserve the selection using this answer:

https://stackoverflow.com/a/3288215/96100

You could instead use my jQuery plug-in, which uses the same code. Example:

var $input = $("#UserUsername");
$input.keyup(function() {
    var sel = $input.getSelection();
    replaze(this);
    $input.setSelection(sel.start, sel.end);
});
Community
  • 1
  • 1
Tim Down
  • 318,141
  • 75
  • 454
  • 536
0

Check this out This example help you to move cursor by arrow keys https://jsfiddle.net/RaviMakwana/a5ts0emr/

<script async src="//jsfiddle.net/RaviMakwana/a5ts0emr/embed/"></script>
Ravi Makwana
  • 2,782
  • 1
  • 29
  • 41