...

View Full Version : keyUp on 4th Press



tomyknoker
08-25-2007, 05: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>

PremiumBlend
08-25-2007, 06:59 PM
Can you replace


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

with


if(postCode.length != 3 || postCode.length != 4) return;

glenngv
08-27-2007, 07:21 AM
Can you replace


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

with


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.

if(postCode.length != 3 && postCode.length != 4) return;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum