Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-25-2007, 05:49 PM   PM User | #1
tomyknoker
Regular Coder

 
Join Date: Mar 2006
Posts: 459
Thanks: 3
Thanked 0 Times in 0 Posts
tomyknoker is an unknown quantity at this point
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>
tomyknoker is offline   Reply With Quote
Old 08-25-2007, 06:59 PM   PM User | #2
PremiumBlend
Regular Coder

 
PremiumBlend's Avatar
 
Join Date: Apr 2006
Location: Marion, Iowa
Posts: 201
Thanks: 0
Thanked 13 Times in 13 Posts
PremiumBlend is an unknown quantity at this point
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
PremiumBlend is offline   Reply With Quote
Old 08-27-2007, 07:21 AM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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;
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:46 PM.


Advertisement
Log in to turn off these ads.