Determine cursor position on textfield

Discussion in 'Javascript' started by Cah Sableng, Mar 7, 2007.

  1. Cah Sableng

    Cah Sableng Guest

    Hi all.
    I am new member of this group.

    I have a problem with currency formatter function I made. On onkeyup's
    method of a textfield I add function below:

    <snip>
    function formatCurrency(elm) {
    var str=elm.value;
    var re = /(-?\d+)(\d{3})(,\d*)?/
    var re1 = /\./g

    str = str.replace(re1,'')
    arr = str.split(',')
    str = arr[0]

    while (re.test(str))
    str = str.replace(re, '$1.$2$3')

    str += (arr.length > 1) ? (',' + arr[1]) : '';

    elm.value = str
    }
    </snip>

    It works as I expected, except that after the function finished its
    execution the cursor moves to the end of the textfield. So if I want
    to insert two or more characters, the rest of it goes to the end of
    textfield.
    How I determine the position of the cursor in the textfield before
    executing the regex?

    Sorry for the bad english I wrote.
    TIA.
    Cah Sableng, Mar 7, 2007
    #1
    1. Advertising

  2. Cah Sableng

    Elegie Guest

    Cah Sableng wrote:

    Hello,

    <snip>

    > How I determine the position of the cursor in the textfield before
    > executing the regex?


    With 'el' being your form control, try using :

    function getCaretPos(el) {
    var rng, ii=-1;
    if(typeof el.selectionStart=="number") {
    ii=el.selectionStart;
    } else if (document.selection && el.createTextRange){
    rng=document.selection.createRange();
    rng.collapse(true);
    rng.moveStart("character", -el.value.length);
    ii=rng.text.length;
    }
    return ii;
    }

    You'll find plenty of examples in the newsgroup archives, located at
    <URL:http://groups.google.fr/group/comp.lang.javascript/topics>


    HTH,
    Elegie.
    Elegie, Mar 7, 2007
    #2
    1. Advertising

  3. Cah Sableng

    Cah Sableng Guest

    On Mar 7, 9:04 pm, Elegie <> wrote:
    > Cah Sableng wrote:
    >
    > Hello,
    >
    > <snip>
    >
    > > How I determine the position of the cursor in the textfield before
    > > executing the regex?

    >
    > With 'el' being your form control, try using :
    >
    > function getCaretPos(el) {
    > var rng, ii=-1;
    > if(typeof el.selectionStart=="number") {
    > ii=el.selectionStart;
    > } else if (document.selection && el.createTextRange){
    > rng=document.selection.createRange();
    > rng.collapse(true);
    > rng.moveStart("character", -el.value.length);
    > ii=rng.text.length;
    > }
    > return ii;
    >
    > }
    >
    > You'll find plenty of examples in the newsgroup archives, located at
    > <URL:http://groups.google.fr/group/comp.lang.javascript/topics>
    >
    > HTH,
    > Elegie.


    Thank you very much. That's what I'm looking for.
    Cah Sableng, Mar 8, 2007
    #3
  4. Cah Sableng

    Lee Guest

    Cah Sableng said:
    >
    >Hi all.
    >I am new member of this group.
    >
    >I have a problem with currency formatter function I made. On onkeyup's
    >method of a textfield I add function below:
    >
    >It works as I expected, except that after the function finished its
    >execution the cursor moves to the end of the textfield. So if I want
    >to insert two or more characters, the rest of it goes to the end of
    >textfield.


    That's a good example of why you should wait until the onchange
    event handler to reformat the field. Reformatting after each
    keystroke is inefficient and is annoying to the user.


    --
    Lee, Mar 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VG9tYXMgS2VwaWM=?=

    Changing DEFAULT cursor to WAIT cursor in ASP

    =?Utf-8?B?VG9tYXMgS2VwaWM=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,293
    Bruce Barker
    Apr 5, 2005
  2. tom arnall
    Replies:
    6
    Views:
    1,261
    Ian Shef
    Jan 18, 2007
  3. Replies:
    1
    Views:
    1,456
    Brandon McCombs
    Feb 24, 2007
  4. invy
    Replies:
    4
    Views:
    379
    CBFalconer
    Dec 28, 2006
  5. dmaziuk
    Replies:
    3
    Views:
    554
    Chris Gonnerman
    Jan 25, 2011
Loading...

Share This Page