如何使这个javascript验证工作?

时间:2012-12-31 09:48:23

标签: javascript jquery ajax

validateMail()下,变量chump返回undefined,但它应该是true或false值。

我不明白,因为alert()的两个条件语句下的finalFlash()语句工作正常,我在那里得到了真值或假值。

<script>
function validateRecipient()
{
var recipient=document.messageForm.recipient.value;

if (recipient==null || recipient=="")
  {
  document.getElementById("recipientError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">Please enter a username</div>';
  document.getElementById("recipient_error").className="control-group error";
  return false;
  }

else
  {
  document.getElementById("recipientError").innerHTML="";
  document.getElementById("recipient_error").className="control-group";
  return true;
  }  

}

function validateMessage()
{
var message=document.messageForm.message.value;
if (message==null || message=="")
  {
  document.getElementById("messageError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">Please enter a message</div>';
  document.getElementById("message_error").className="control-group error";
  return false;
  }

else
  {
  document.getElementById("messageError").innerHTML="";
  document.getElementById("message_error").className="control-group";
  return true;
  }    

}


function validateMail()
{
var items = [validateRecipient(), validateMessage(), validateUser()];
var chump = validateUser()
alert(chump)

for (var i in items)
   {
   var item = items[i];
   item
   }

if (validateRecipient() && validateMessage() && validateUser())
   {
   return true;
   }

return false;
}


function validateUser(){
    $.get("/trivia/xhr_test/",
    {
      username: document.messageForm.recipient.value
    },
    function(data){
      return finalFlash(data); // edit suggested by Aamir Adnan
    });
}

function finalFlash(data){
    if (data == "True")
      {
      document.getElementById("recipientError").innerHTML="";
      document.getElementById("recipient_error").className="control-group";
      alert(true)
      return true
      }

    else if (data != null && data != "" && data == "False")
      {
      document.getElementById("recipientError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">That username does not exist</div>';
      document.getElementById("recipient_error").className="control-group error";
      alert(false)
      return false
      }
}



</script>

2 个答案:

答案 0 :(得分:2)

您的validateUser函数启动异步获取请求并立即返回。 get完成后调用finalFlash,但与validateUser的返回值无关。

请参阅How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?了解如何将其转换为同步请求。

答案 1 :(得分:0)

希望此代码可以帮助您

<script>
function validateRecipient()
{
var recipient=document.messageForm.recipient.value;

if (recipient==null || recipient=="")
  {
  document.getElementById("recipientError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">Please enter a username</div>';
  document.getElementById("recipient_error").className="control-group error";
  return false;
  }

else
  {
  document.getElementById("recipientError").innerHTML="";
  document.getElementById("recipient_error").className="control-group";
  return true;
  }  

}

function validateMessage()
{
var message=document.messageForm.message.value;
if (message==null || message=="")
  {
  document.getElementById("messageError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">Please enter a message</div>';
  document.getElementById("message_error").className="control-group error";
  return false;
  }

else
  {
  document.getElementById("messageError").innerHTML="";
  document.getElementById("message_error").className="control-group";
  return true;
  }    

}


function validateMail()
{
  validateUser();
}

function validateAll(chump){

var items = [validateRecipient(), validateMessage()];

for (var i in items)
   {
   var item = items[i];
   item
   }

if (validateRecipient() && validateMessage() && chump)
   {
   return true;
   }

return false;

}
function validateUser(){
    $.get("/trivia/xhr_test/",
    {
      username: document.messageForm.recipient.value
    },
    function(data){
      validateAll(finalFlash(data)); // edit suggested by Aamir Adnan
    });
}

function finalFlash(data){
    if (data == "True")
      {
      document.getElementById("recipientError").innerHTML="";
      document.getElementById("recipient_error").className="control-group";
      alert(true)
      return true
      }

    else if (data != null && data != "" && data == "False")
      {
      document.getElementById("recipientError").innerHTML='<div style="font-size:13px; color:#b94a48; clear:both">That username does not exist</div>';
      document.getElementById("recipient_error").className="control-group error";
      alert(false)
      return false
      }
}



</script>

此脚本可帮助您完成所需操作。你做的错误是你在validateUser方法上调用了ajax,并且你已经调用了两次。每次激活ajax请求时,它都将异步执行,其余代码永远不会等待ajax调用完成。所以我做了必要的更改,允许代码在完成ajax请求后执行。