Results 1 to 9 of 9

Thread: keyUp on 4th Press

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keyUp on 4th Press

    I have a AJAX script which searches postcodes in my database to find the nearest store... Works really well... Initially the postcodes were 4 characters long but a few are actually 3 characters long, and the script only searches the database once the 4th key is pressed is there anyway to make it work on the 3rd key press AND the 4th? My code is as follows:

    Code:
    <script src="http://yui.yahooapis.com/2.2.2/build/yahoo-dom-event/yahoo-dom-event.js" type="text/javascript"></script>
    <script src="http://yui.yahooapis.com/2.2.2/build/connection/connection-min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var $D = YAHOO.util.Dom;     //YAHOO DOM
    var $E = YAHOO.util.Event; //YAHOO Event
    var $C = YAHOO.util.Connect; //YAHOO connection manager
     
    function init() {
        $E.on('Code', 'keyup', chkZip);
    	$E.on('findStore', 'submit', function(e) {$E.stopEvent(e);});
    }
     
    //See if code is fully entered (4 digits)
    function chkZip(e) {
        var postCode = $E.getTarget(e).value;
        if(postCode.length < 4) return; //Ignore if not complete
        
        var storeSpan = document.getElementById('store'); //Element to put store name into
        var AjaxObj = {
            success: function(o) {
                storeSpan.innerHTML = o.responseText;
            },
            failure: function(o) {
                storeSpan.innerHTML = "<em>Error - Please try again</em>";
            },
            timeout: 5000
        }
        $C.asyncRequest('GET', 'findMyRep.php?postCode=' + encodeURIComponent(postCode), AjaxObj);
                
    }
    $E.onDOMReady(init);
    </script>

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Change this line:

    Code:
    if(postCode.length < 4) return; //Ignore if not complete
    to this:

    Code:
    if(postCode.length < 3) return; //Ignore if not complete
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Then it will fire on 1, 2, and 3, but not 4.

    Code:
    if(postCode.length === 3 || postCode.length === 4)
    would probably be easiest.

    /FURTHER EDIT: That's just silly. Why did I write that?
    Last edited by Twey; 08-25-2007 at 10:15 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey Twey I tried that but it still will only fire on the 4th key up... Any ideas?

  5. #5
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Also this is the line I have on my php page which the form get's posted too on the 4th key up...

    PHP Code:
    if(!isset($_GET['postCode']) OR !preg_match('/^[0-9]{4}$/'$_GET['postCode'])) exit('Invalid Postcode'); 

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm just guessing but:

    PHP Code:
    {4
    might need to be:

    PHP Code:
    {3,4
    Code:
    if(postCode.length < 3) return; //Ignore if not complete
    Quote Originally Posted by Twey
    Then it will fire on 1, 2, and 3, but not 4.
    Actually, it will return (do nothing) on 1 and 2, if the length is 3 or greater, it will continue processing.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, I missed out a minus sign that was rather important. Edited.
    Actually, it will return (do nothing) on 1 and 2, if the length is 3 or greater, it will continue processing.
    There was an edit: it originally said <= 3. I meant that the if block would fire.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by Twey View Post
    There was an edit: it originally said <= 3. I meant that the if block would fire.
    Yea, I realized I put that there and took it out imediately. You must have been reading it as I was editting.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It is working now guys thanks! But is a bit slow on the keyUp now that it also checks on the 3rd keyUp... So if a user types 2000, the first time it says Invalid Postcode, if you retype it works... Is that just a glitch I have to put up with?

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
  •