2012-12-31 99 views
0

validateMail()下,變量chump正在返回undefined,但它應該是true或false值。如何使這個JavaScript驗證工作?

我不明白,因爲finalFlash()這兩個條件語句下的語句alert()工作正常,我在那裏得到一個真或假的值。

<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

丟失return'finalFlash(data);'應該'返回finalFlash(data);'in'validateUser'函數 –

+0

@AamirAdnan不僅如此。 'return'將返回到匿名函數,而不是'validateUser'。 @PinkiePie你將不得不亂用回調。 – Chris

+0

然後可以將'chump'定義爲一個全局變量,然後用它來代替返回值來覆蓋全局變量。 –

回答

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請求後執行。

+0

謝謝,但這不起作用 –

+0

其實當你將調用validateMail函數 – AmGates