2

I'm trying to check if email exists with jQuery, but works only when typing, when I try with copy paste than shows that email is available, but its not. Please see below code.

JavaScript:

$(document).ready(function(){
    $('#test').keyup('check.php').show();
    $('#sign_up_email_input_first').keyup(function(){
        $.post('check.php', {email_sign_up: form.email_sign_up.value}, function(result){
            $('#test').html(result).show();}
        ); 
    });
});

PHP:

if(isset($_POST['email_sign_up']))
{
    $email = $_POST['email_sign_up'];
    $select = mysql_query("SELECT email FROM users WHERE email = '$email'") or die(mysql_error());
    $count = mysql_num_rows($select);
    if($email == NULL){
        echo "Choose Username";
    }else if(strlen($email)<=3){
        echo "";
    }else{
        if($count == 0){
            echo "Username is Available";
        }else if($count ==1){
            echo "Username is not Available";
        }
    }
}
Ultimater
  • 4,647
  • 2
  • 29
  • 43
K.Wayne
  • 87
  • 10

2 Answers2

0

The reason it only works when typing is you're using the keyup event to capture input.

pabrams
  • 1,157
  • 10
  • 34
  • 1
    Depends what you want to achieve exactly. Maybe blur? – pabrams Jun 21 '16 at 01:24
  • 1
    why not register onblur as well? This way, the email will be validated once the user finishes copy and pasting and clicks elsewhere – derp Jun 21 '16 at 01:25
  • I have changed 'onblur', but is not showing anything! – K.Wayne Jun 21 '16 at 01:28
  • @K.Wayne See [catch paste input](http://stackoverflow.com/questions/686995/catch-paste-input) – Ultimater Jun 21 '16 at 01:32
  • @K.Wayne Also, your question could use some more work - I'm too lazy to do it for you. You should probably read the help center article "How to create a minimal, complete and verifiable example". – pabrams Jun 21 '16 at 01:43
0

keyup is only triggered when the user stokes any of the keyboard input... So when you use mouse events like copy and paste it isn't triggered.

try using change instead of keyup

Nishanth Matha
  • 5,993
  • 2
  • 19
  • 28