使用Live Validation.Custom功能检查用户名(电子邮件)可用性

时间:2013-12-11 20:19:04

标签: javascript php jquery ajax

我在使用LiveValidation的自定义功能检查电子邮件可用性时遇到问题:它会不断发回电子邮件已被使用,即使它不是。

有人可以帮我解决这个问题吗?

- 编辑 -

我发现函数check_availability无法从Ajax调用中返回true或false。所以我几乎在那里我只需要让函数返回正确的bool值。

这是我的代码,直到现在:

JS文件:

//function to check username availability  
    var check_availability = function(){  

        //get the username  
        var email = $('#email').val();  

        //use ajax to run the check  
        $.post("checkEmail.php", { email: email },  
            function(result){  
                //if the result is 1  
                if(result == 1){  
                    //show that the username is available  
                    return true;
                }else{  
                    //show that the username is NOT available  
                    return false;  
                }  
        }); 
// Validation 
var mail =  new LiveValidation('email', { validMessage: "Ok!" });
mail.add( Validate.Custom,{failureMessage:"E-mail is al in gebruik!", 
                 against: function(){ return check_availability() }
                 });

checkEmail.php档案:

<?php
require_once 'db_config.php';
//Controleren of e-mail adress al in gebruik is     

$sql_select_email = "SELECT email from tblUsers WHERE email = '".mysql_real_escape_string($_POST['email'])."'";
            if (($result_select_email = mysql_query($sql_select_email)) === false) 
            {
                # als de query fout is -> foutafhandeling
                echo showSQLError($sql_select_email,mysql_error(),'Fout bij het opvragen van de gegevens.');
            }
            else
            {
                //Query gelukt
                $count_email = mysql_num_rows($result_select_email);
                if($count_email > 0)
                {
                    // Not available
                    echo 0; 
                }
                else
                {
                    // Available
                    echo 1; 
                }
            }
?>

2 个答案:

答案 0 :(得分:0)

如果我没弄错,ajax结果将是一个字符串。 result ==“1”not result == 1.

你也可以做结果== parseFloat(结果)

答案 1 :(得分:0)

试试这个..

//function to check username availability  
check_availability = function(){  

    //get the username  
    var email = $('#email').val();

    //declare the return value for the $.post call
    var return_value = null;

    // make the $.post call synchronous  !!important
    $.ajaxSetup({async: false});

    //use ajax to run the check  
    $.post("checkEmail.php", { email: email },  
        function(result){  
            //if the result is 1  
            if(result == 1){  
                //show that the username is available  
                return_value = true;
            }else{  
                //show that the username is NOT available  
                return_value = false;  
            }  
    }); 

    // return the result (true or false)
    return return_value;

// Validation 
var mail =  new LiveValidation('email', { validMessage: "Ok!" });
mail.add( Validate.Custom,{failureMessage:"E-mail is al in gebruik!", 
             against: function(){ return check_availability() }
             });

这应该有效。我测试了它。有点......哈!

问题是您无法从$ .post调用范围内返回值。您必须首先在$ .post调用范围之外声明返回变量。然后,您可以在$ .post中分配该变量的值。但是$ .post调用也必须设置为“同步”。即 - $.ajaxSetup({async: false});