Skip to content Skip to sidebar Skip to footer

Else If Statement In Javascript Not Able To Display Validation Message

I am having trouble displaying strings depending on the if/else statements in my validation. If you look at the code below, if the if statement is met, then it displays the message

Solution 1:

Rephrase your JavaScript this way:

functioneditvalidation() {
    var isDataValid = true;
    var currentAssesO = document.getElementById("currentAssessment");
    var noStudentsO = document.getElementById("addtextarea");
    var studentAssesMsgO = document.getElementById("studentAlert");
    var errorMsg = "";

    studentAssesMsgO.innerHTML = "";
    if (currentAssesO.value == "" || noStudentsO.value == "") {
        $('#targetdiv').hide();
        isDataValid = false; 

        if (currentAssesO.value == "") {
            errorMsg += "Please Select an Assessment to edit from the Assessment Drop Down Menu";
        }

        if (noStudentsO.value == "") {
            errorMsg += "You have not Selected any Students you wish to Add into Assessment";
        }

        studentAssesMsgO.innerHTML = errorMsg; // Plus whatever styling for messages.
    }

    return isDataValid;
}

Updated answer

Please include jQuery by putting this in the <head> section.

<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

Update your script:

functioneditvalidation()
{
    if ($("#sessionsDrop").val()=="")
        $("#studentAlert").html("Assessment needs to be filled.");
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length==0)
        $("#studentAlert").html("No students have been selected to add to assessment.");
    if ($("#sessionsDrop").val()!="" && $("#studentadd").children().length!=0)
        returntrue;
    returnfalse;
}

Solution 2:

Here is the magic:

else {
    studentAssesMsgO.innerHTML = ""; 
    alert(noStudentsO.value); //tell me why I'm in this block
}

Post a Comment for "Else If Statement In Javascript Not Able To Display Validation Message"