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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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
    Regular Coder PremiumBlend's Avatar
    Join Date
    Apr 2006
    Location
    Marion, Iowa
    Posts
    201
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Can you replace

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

    Code:
    if(postCode.length != 3 || postCode.length != 4) return;
    My Website: DumpsterDoggy

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by PremiumBlend View Post
    Can you replace

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

    Code:
    if(postCode.length != 3 || postCode.length != 4) return;
    Condition should be AND not OR, otherwise, length of 3 or 4 will always satisfy one of the conditions.
    Code:
    if(postCode.length != 3 && postCode.length != 4) return;


  •  

    Posting Permissions

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