Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

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 01-11-2013, 07:04 AM   PM User | #1
Pervez
New Coder

 
Join Date: Jul 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Pervez is an unknown quantity at this point
Wait till user finish typing in input filed

Hi,


Am trying to get some data from DB , here am checking whether username is registered or not , if he register am getting some data related to that user and displaying on window .

I want function to wait for untill user finish typing his username ,
then run the function , Below is my code ,
Code:
$(function (){  
        var minlength = 2;                        
        $("#username").keyup(function () {
        var that = this,
        value = $(this).val();          
        if (value.length >= minlength ) {
                $.ajax({                
                        type: "GET",
                        url: "Path_to_php",
                        data: {'username' : value},
                        dataType: "html",
                        success: function(data){
                                if (value==$(that).val()) {
                                        $("#content").append("<p>" + data + "</p>");
                                }
                                else {
                                        alert ("Username Not Registered");
                                }
                        }
                });
                }
        });
});
Here , when user start entering his name , that time only Function getting call for each char he entered
Pervez is offline   Reply With Quote
Old 01-11-2013, 10:32 AM   PM User | #2
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
niralsoni is an unknown quantity at this point
Its because you are invoking the "onKeyUp" event that the function call starts immediately.

Well, "OnKeyUp" is the right event, but how your code will come to know that user has finished entering the text ?

For that you need to track a specific key. If that key is hit, that means user has finished entering the text. Normally we call this specific key as the "Enter key".

So, onkeyup, you track which key was pressed. If its "Enter key", then only you start processing your code.

something like -
Code:
var key = event.which || event.keyCode;
if(key == 13) { // 13 is the keycode for enter key
// your piece of code here...
}
Hope it helps you out...

Regards,
Niral Soni
niralsoni is offline   Reply With Quote
Old 01-11-2013, 11:15 AM   PM User | #3
Pervez
New Coder

 
Join Date: Jul 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Pervez is an unknown quantity at this point
Hey ,

Thank you for your time ,

I didn't how to do that ............

Please can you edit my code , Below is JSfiddle link

http://jsfiddle.net/ZvNFc/
Pervez is offline   Reply With Quote
Old 01-11-2013, 09:46 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
An easier way: Just use "onchange" instead of "onkeyup".

That is, when the browser detects the user has finished changing the field--when the user hits the TAB key or the ENTER key or mouse clicks to a different field *AND* the contents of the field have *changed*--*THEN* do your AJAX actions.

SO just change
Code:
        $("#username").keyup(function () {
to
Code:
        $("#username").change(function () {
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Pervez (01-13-2013)
Old 01-15-2013, 04:40 AM   PM User | #5
Pervez
New Coder

 
Join Date: Jul 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Pervez is an unknown quantity at this point
Smile

Thank You buddy
Pervez is offline   Reply With Quote
Reply

Bookmarks

Tags
jquery jaascript

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 10:53 AM.


Advertisement
Log in to turn off these ads.