简体   繁体   English

如何使这个javascript验证工作?

[英]How do I make this javascript validation work?

Under validateMail() , the variable chump is returning undefined , but it should be either a true or false value. validateMail() ,变量chump返回undefined ,但它应该是true或false值。

I don't understand, because the alert() statements under the two conditional statements of finalFlash() work fine, I get a true or false value there. 我不明白,因为finalFlash()的两个条件语句下的alert()语句工作正常,我得到一个true或false值。

<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>

Your validateUser function starts an asynchronous get request and returns right away. 您的validateUser函数启动异步get请求并立即返回。 finalFlash is called when the get completes, but has nothing to do with the return value of validateUser. get完成后调用finalFlash,但与validateUser的返回值无关。

See How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? 请参阅如何让jQuery执行同步而非异步的Ajax请求? for how to convert it to a synchronous request. 如何将其转换为同步请求。

Hope This code helps you 希望这段代码可以帮到你

<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>

Here this script will help you do what you want. 这个脚本将帮助您做您想做的事。 The mistake you have done is that u had ajax call on validateUser method and you have called it twice. 你做的错误是你在validateUser方法上调用了ajax,并且你已经调用了两次。 Each time when the ajax request is fired it will act asynchronously and the remaining code will never wait for the ajax call to get complete. 每次激活ajax请求时,它都将异步执行,其余代码永远不会等待ajax调用完成。 So I have made necessary changes that will allow the code to execute after the completion of the ajax request. 所以我做了必要的更改,允许代码在完成ajax请求后执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM