Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Trapping the Cursor Position in a text box

    Does anyone know how to trap or calculate the character position of the cursor within a text box or text area?

    When I do my validation if the text contains an illegal character it returns it's position, but if you have a large amount of data it can be difficult to find the problem without manually counting through the text, which can be extremely tedious.

    Any help would be greatly appreciated.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    226
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so you want the user to get an alert or something of that nature AS he is typing....

    In other words... The second the user inserts an invalid character you want that character to trigger some sort of event...

    Is this what you mean?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var illegal = ["%","*","^","#","@"];
    function checkChar(evt) {
    var e = new Object();
    e.which = (evt.which) ? evt.which : event.keyCode;
    var char = String.fromCharCode(e.which);
    for(var i = 0; i < illegal.length; i++) {
    if(illegal[i] == char) return i;
    }
    }
    Then, add an event handler for onkeydown to your input field:

    Code:
    <input onkeydown="checkChar(event);" />
    Hope that helps!

    Happy coding!

  • #4
    Regular Coder Running Bear's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    104
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Squintz

    Sorry for not making my question clear.

    I'm looking for a counter which will tell me how many characters there are to the left of the cursor position.

    -------------------------------

    nolachrymose


    Thanks for the function, unfortunately I can't seem to get it to work. If I can't get the character counter as described above this would be a good substitute.

    Regardless of what key is pressed I get 4 or 5 alerts. I'm not really sure what it's supposed to do exactly. I was expecting only the illegal characters to go through the loop?

    What is supposed to happen when it traps an illegal char?

    Code:
    <script language="JavaScript">
    var illegal = ["%","*","^","#","@","'"];
    function checkChar(evt) {
    var e = new Object();
    
    e.which = (evt.which) ? evt.which : event.keyCode;
    var char = String.fromCharCode(e.which);
    for(var i = 0; i < illegal.length; i++) {
    if(illegal[i] == char) return i;
    	alert("I'm getting all the way through")
    }
    }
    </script>
    Cheers Al


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •