简体   繁体   中英

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.

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.

<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. finalFlash is called when the get completes, but has nothing to do with the return value of validateUser.

See How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request? 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. 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. So I have made necessary changes that will allow the code to execute after the completion of the ajax request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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