SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trigger event only upon entry of alphabetic character

    I've got a text input field to which I've added some autocomplete functionality using jQuery and AJAX. The function which loads the autocomplete script is currently triggered every time a key is pressed, but I want to refine this so that the only keystrokes which trigger the search are alphabetic ones.

    The reason I want to do this is that pressing, for example, backspace, still triggers the autocomplete which I don't want.

    My current code looks like this:

    Code javascript:
    $("#searchbox").keyup(function() {
        var inputbox = $(this);
        var keyword = inputbox.val();
        var cursorPos = country.length;
        if(cursorPos > 1)
        {
            var result;
            $.get("http://www.example.com/autocomplete.asp", { 'keyword': keyword }, function(data) {
                // insert data into textbox
            });
        }
    });

    Any help or input would be much appreciated

  2. #2
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,287
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    if(cursorPos > 1)
    {
    var result; ...

    somewhere there, you're already checking for something before calling your asp script. So, I dunno jQuery and am a JS n00b but you'd want a regex there as well.

    var alphaChar=/\w+/; in your if statement also ask if the input being typed matches this variable.
    if(cursorPos>1 && (the match is true)) { etc...

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I can't use regex to detect whether the Ctrl or the Backspace key is pressed though unfortunately.

  4. #4
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,287
    Mentioned
    51 Post(s)
    Tagged
    2 Thread(s)
    Don't do key detection, have the script look at what's been typed (that is, look at the string that appears).

    I kinda had something where as users typed into a textarea, a span showed how many characters they typed. Now I had the script check the value of the textarea onkeyup, but I didn't count keys (otherwise the span would have counted every backspace and shift etc). I only did a textarea.value.length.
    Similarly I think onkeyup you should just look at the current value of the input the person is typing in (so using this). That string you can put through a regex, and avoids seeing any weird keys that don't actually type anything. If the key that is let up is shift or ctrl or whatever, sure the script will take a look at your string, but since nothing was typed or added to the input it doesn't matter. The only important thing is, what characters are in the string of the input's value? If nothing (" "), or if non-alphabetical, do not run the asp script.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I agree with the stupid cat in that you should be checking the actual input (for instance, should the user paste something using the mouse - this means you'll need to ensure your function runs onchange rather than simply onkeyup), but still, if you really want to check that an alphanumeric key was pressed:

    Code javascript:
    function(e) {
      e = e || window.event;
      kc = e.which || e.keyCode;
      if (kc > 64 && kc < 91) {
        //keyCode is [a-z]
      }
    }

    keyCodes

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Raffles, that code worked perfectly (with the addition of the keycode for the space character which I realised I'd probably need)

    Hmmm, I never thought of the implications of a copy+paste type scenario. When I'm back in work tomorrow I'll try using onchange instead of onkeyup - thanks for the input, guys.


Bookmarks

Posting Permissions

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