0

我通过使用 php 验证可用性为电子邮件检查编写了一些 ajax 验证,ajax 脚本只显示电子邮件是否可用?

<script type="text/javascript">

$(document).ready(function()
{
$("#email_id").change(function(){

var email = $("#email_id").val();
    var regdata = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     if(!(regdata).test($("#email_id").val()))
     {

     $("#email_id").css('border','1px solid red');
     $("#email_id").focus();
     $("#status").html("enter the valid emailid!");
      return false;
     }
    else{
       $("#email").css('border','1px solid #7F9DB9');
      $("#email_id").html('Checking Email Availability...');

     $.ajax({ 
     type: "POST", 
     url: "fresherreg_email_avail.php", 
     data:"q="+ email, 
     success: function(server_response){
    $("#status").ajaxComplete(function(event,request){

    if(server_response == '0')
    {
    $("#status").html('Email Available');

    } 
    else  if(server_response == '1')
    { 

    $("#status").html('Email Not Available');

    } 
     });
   }
  });
 }
});
});
</script>

我的 php 可用性检查代码是

<?php
     include_once("include_dao.php");

    $q = $_REQUEST['q'];
    if($q != "")
    {
        $row=DAOFactory::getTblFreshersRegistrationDAO()->queryByEmailId($q);

        $num = count($row);
        if($num > 0)
        {
          echo "1";
        }
        else
        {
            echo "0";
        }
    }
    else
    {
        echo "Email Id should not be empty";
    }
?>

我需要的是什么?它应该使用脚本显示警报,直到他选择新的邮件 ID

4

2 回答 2

2

可以这样做。我将编辑您的代码并添加所需的东西。

在 javascript/jquery 部分:

<script type="text/javascript">


$(document).ready(function()
{
$("#email_id").change(function(){

var email = $("#email_id").val();
    var regdata = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     if(!(regdata).test($("#email_id").val()))
     {

     $("#email_id").css('border','1px solid red');
     $("#email_id").focus();
     $("#status").html("enter the valid emailid!");
      return false;
     }
    else{
       $("#email").css('border','1px solid #7F9DB9');
      $("#email_id").html('Checking Email Availability...');

     $.ajax({ 
     type: "POST", 
     url: "fresherreg_email_avail.php", 
     data:"q="+ email, 
     success: function(server_response){
             if(server_response == '0')
             {
                $("#status").append("<font color='green'>email available</font>");

             } 
             else  if(server_response == '1')
             { 

               $("#status").append("<font color='red'>email already exits</font>");
             }
             else
             { 
               $("#status").append("<font color='red'>"+server_response+"</font>");
             }

     }

     });
 }
});

});
</script>

现在在你的 php 脚本中。

<?php
     include_once("include_dao.php");

    $q = $_REQUEST['q'];
    if($q != "")
    {
        $row=DAOFactory::getTblFreshersRegistrationDAO()->queryByEmailId($q);

        $num = count($row);
        if($num > 0)
        {

        echo "1";
        }
        else
        {

             echo "0";
        }
    }
    else
    {
        echo "Email Id should not be empty";
    }
?>
于 2013-02-25T10:35:17.240 回答
1
<script type="text/javascript">

$(document).ready(function(){
        $('#submit');
        var emaildone = false;

        var myRegForm = $("#registration"),email = $("#email_id"), status = $("#status");
         myRegForm.submit(function(){
                if(!emaildone)
                {
                        alert("Email Id Already Exists!!! So Please Try Another Email Id");
                        email.attr("value","");
                        email.focus();
                        return false;
                }
        });


        email.blur(function(){
                $.ajax({
                        type: "POST",
                        data: "q="+$(this).attr("value"),
                        url: "fresherreg_email_avail.php",
                        beforeSend: function(){
                                status.html('<img src="images/loader.gif" align="absmiddle"><font color="blue">Checking Email Availability...</font>');
                        },
                        success: function(data){
                                if(data == "invalid")
                                {
                                        emaildone = false;
                                        status.html("<font color='red'>Inavlid Email!! Please Select a Vaild Email</font>");
                                }
                                else if(data != "0")
                                {
                                        emaildone = false;
                                        status.html('<img src="images/not_available.png" align="absmiddle"><font color="red">Email Already Exist</font>');
                                }
                                else
                                {
                                        emaildone = true;
                                        status.html('<img src="images/available.png" align="absmiddle"> <font color="green">Email Available</font>');
                                }
                        }
                });
        });
});

</script>

将脚本更改为此。

于 2013-02-25T10:15:16.380 回答