Log in

View Full Version : keyUp on 4th Press



tomyknoker
08-25-2007, 04:49 PM
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:


<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>

thetestingsite
08-25-2007, 05:01 PM
Change this line:



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


to this:



if(postCode.length < 3) return; //Ignore if not complete


Hope this helps.

Twey
08-25-2007, 05:17 PM
Then it will fire on 1, 2, and 3, but not 4.


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

/FURTHER EDIT: That's just silly. Why did I write that?

tomyknoker
08-25-2007, 05:29 PM
Hey Twey I tried that but it still will only fire on the 4th key up... Any ideas?

tomyknoker
08-25-2007, 05:41 PM
Also this is the line I have on my php page which the form get's posted too on the 4th key up...


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

jscheuer1
08-25-2007, 06:08 PM
I'm just guessing but:



{4}

might need to be:


{3,4}



if(postCode.length < 3) return; //Ignore if not complete

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.

Twey
08-25-2007, 08:13 PM
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.

thetestingsite
08-25-2007, 08:50 PM
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.

tomyknoker
08-31-2007, 09:16 AM
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?