Php 自定义jQuery验证

Php 自定义jQuery验证,php,jquery,jquery-validate,Php,Jquery,Jquery Validate,我正在尝试jQuery验证。我的代码是: jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { var emailVal; $.getJSON('get/getAllData.php?operation=email&email='+value, null, function(data) { alert(data); if(data==0)

我正在尝试jQuery验证。我的代码是:

jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 
  var emailVal;
    $.getJSON('get/getAllData.php?operation=email&email='+value, null, function(data) {
       alert(data);
       if(data==0)
          emailVal true;
       else
          emailVal false;
    });
    return emailVal;
  }, "My message");
如果返回值为true,我想显示我的消息。但是这个代码是错误的。我的信息会持续显示

如果我按以下方式更改代码:

jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

        return false;

      }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

            return true;

          }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element) { 
   var emailVal=true;
   $.ajax({
       url: 'get/getAllData.php?operation=email&email='+value,
       type:'GET',
       dataType: 'json',
       async: false,
       success: function (data) {
            if(data==0)
               emailVal=true;
            else
               emailVal=false;
            },

        });   
   return emailVal;
}, "My Message");
显示我的消息

如果我按以下方式更改代码:

jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

        return false;

      }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

            return true;

          }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element) { 
   var emailVal=true;
   $.ajax({
       url: 'get/getAllData.php?operation=email&email='+value,
       type:'GET',
       dataType: 'json',
       async: false,
       success: function (data) {
            if(data==0)
               emailVal=true;
            else
               emailVal=false;
            },

        });   
   return emailVal;
}, "My Message");
我的信息不显示

有什么想法吗

HTML代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/jquery.js"><\/script>')</script>
        <script language="javascript" type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

        <!-- Bootstrap jQuery plugins compiled and minified -->
        <script type="text/javascript" src="./js/jquery.validate.js"></script>
        <script>
            $(document).ready(function(){

                jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 
                    var emailVal;

                    $.getJSON('get/getAllData.php?operation=email&email='+value, null, function(data) {
                        alert(data);
                        if(data==0)
                            emailVal true;
                        else
                            emailVal false;
                    });
                    return emailVal;
                }, "My Message");

                $("#signupform").validate({
                    rules:{
                        email:"isOnlyEmail"
                    },

                    highlight: function(label) {
                        $(label).closest('.control-group').removeClass('success');
                        $(label).closest('.control-group').addClass('error');
                    },
                success: function(label) {
                    label
                    .text('OK!').addClass('valid')
                    .closest('.control-group').removeClass('error')
                    .closest('.control-group').addClass('success');
                }
            });
        });
        </script>

    </head>
    <body>
        <input id="email" type="email" name="email" placeholder="email@site.com" />
    </body>
</html>

window.jQuery | | document.write(“”)
$(文档).ready(函数(){
addMethod(“IsonlyMail”,函数(值、元素、参数){
var-emailVal;
$.getJSON('get/getAllData.php?操作=电子邮件和电子邮件='+值,null,函数(数据){
警报(数据);
如果(数据==0)
真的;
其他的
emailVal假;
});
返回emailVal;
}“我的信息”);
$(“#注册表”).validate({
规则:{
电子邮件:“IsonlyMail”
},
突出显示:功能(标签){
$(标签)。最近('.control group')。removeClass('success');
$(标签)。最近('.control group')。addClass('error');
},
成功:功能(标签){
标签
.text('OK!').addClass('valid'))
.closest('.control group')。removeClass('错误')
.closest(“.control group”).addClass('success');
}
});
});
PHP代码

<?php

    require_once("../db/dbconnect.php");

    $operation = $_GET['operation'];

    if($operation == "email"){

        $mail=$_GET['email'];
        $query = "SELECT * FROM ACCOUNT WHERE email='".$mail."'";
        $result = execute_query($query);
        echo mysql_num_rows($result);
    }
 ?>
我的问题解决了

我更改代码如下:

jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

        return false;

      }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element,param) { 

            return true;

          }, "My message");
jQuery.validator.addMethod("isOnlyEmail", function(value, element) { 
   var emailVal=true;
   $.ajax({
       url: 'get/getAllData.php?operation=email&email='+value,
       type:'GET',
       dataType: 'json',
       async: false,
       success: function (data) {
            if(data==0)
               emailVal=true;
            else
               emailVal=false;
            },

        });   
   return emailVal;
}, "My Message");

通常,ajax调用以异步模式运行。由于异步模式的原因,它无法工作。如果在我的代码中添加了“async:false”,那么问题就解决了。

您是否尝试过在Firebug中调试?是的,我尝试过。getJSON()函数工作正常。请发布其余代码。。。表单和jQuery其余部分的HTML。如果您的测试显示
true
不显示您的消息,而
false
显示您的消息,那么您为什么希望
true
显示消息?我的目标是:如果当前输入的电子邮件地址以前已经使用过,则显示“我的邮件”。