I have setup a jQuery keyup delay function to check the email typed into an input field.

It works fine after testing with an alert('Key pressed!');

But I want it to say, for example, 'Yes this email is associated with a user' OR 'Sorry, this email is not in our database' without submitting any page requests.

    var delay = (function(){
        var timer = 0;
        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
    $('input').keyup(function() {
            // check email exists and output result
            // Can I use PHP here? 
            // Can ajax/json help?
        }, 500 );

I tried to check with php but this is rendered before the jQuery of course which is where I'm storing my email var.

After a few google searches it sounds like this may not be possible. Can anyone confirm or offer a solution?


  • Since the keyup event is fired every time a keyboard key is released, I would recommend using the blur event which fires only when the user moves to the next field. You should be able to attache the event to an ajax call that will check the existence of the email address.
    – nvwd
    Commented Oct 17, 2012 at 11:36
  • 1
    This is not WordPress specific. Commented Oct 17, 2012 at 12:13
  • Perhaps not strictly, but I had the original intention of using <?php if ( email_exists($email) ) { . . . } ?> which is wordpress specific. So I suppose you could generalise my question as, can I use jquery/json/ajax with if ( email_exists($email).
    – zilj
    Commented Oct 17, 2012 at 12:52

1 Answer 1


I would use an AJAX request to a PHP script that does the lookup, which might look something like this on the jQuery side after document ready:

// jquery
$('#email-input').live('change', function() {
    //ajax request
        url: "email_check.php",
        data: {
            'email' : $('#email-input').val()
        dataType: 'json',
        success: function(data) {
            if(data.result) {
                alert('Email exists!');
            else {
                alert('Email doesnt!');
        error: function(data){

With the example above your PHP would have to return json data once its done the email check based on the address, for example your email_check.php would contain:

// get email passed via AJAX
$email = $_GET['email'];

// do check
if ( email_exists($email) ) {
    $response->result = true;
else {
    $response->result = false;

// echo json
echo json_encode($response);

Hope that helps!

  • I was coming to the conclusion I need to use ajax, this looks like a good solution. I'm going to try this out now.
    – zilj
    Commented Oct 17, 2012 at 12:55
  • Sorry, I'm a bit of a noob with ajax. Should the jquery be inside my keyup function? I added an if the email exists as a simple test, to my email_check.php outputting either true or false. This isn't right is it? if ($email) {$response->result = true;} else {$response->result = false;}
    – zilj
    Commented Oct 17, 2012 at 13:14
  • Sorry, I have now solved this thanks to your code. I forgot the url: needed to account for the theme url.
    – zilj
    Commented Oct 17, 2012 at 13:23
  • Damn. Unfortunately, within my email_check.php I wanted to run if ( email_exists($email) ) and then return the true or false value. It's causing a server error and I can't seem to get it to work. I tested it within the origin page and it is working. Why not within the php target page?
    – zilj
    Commented Oct 17, 2012 at 14:09
  • Re: your questions about where to use it, just use it within document ready, no need to put it in your keyup function, this function I've written does just that (on change basically). As for the server error your getting on the PHP page can you provide a link? Can you show the code within the email_exists function? Commented Oct 18, 2012 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.