SharePoint Multiple form Validations

Tags: ,



I have the following JavaScript that I am trying to use. Can someone assist and tell me what is incorrect? I would greatly appreciate any help

<script type="text/javascript" src="/SiteAssets/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery-3.4.1.js"></script>
<script type="text/javascript">
        function PreSaveAction() {
drop1 = $("select[title='Supervisor Completed']").val();
date1 = $("input[title='Supervisor Date Completed']").val();
drop2 = $("select[title='IMO Completed']").val();
date2 = $("input[title='IMO Date Completed']").val();
drop3 = $("select[title='IAM Completed']").val();
date3 = $("input[title='IAM Date Completed']").val();
    if (drop1 !=="Select One..." && date1===""){
        alert("The Supervisor Date Completed is a required element. Please Fill this date out.");
        return false;
    }    
        return true;
}
    if (drop2 !=="Select One..." && date2===""){
        alert("The IMO Date Completed is a required element. Please Fill this date out.");
        return false;
    }    
        return true;
}
    if (drop3 !=="Select One..." && date3===""){
        alert("The IAM Date Completed is a required element. Please Fill this date out.");
        return false;
    }    
        return true;
}
        }
</script>

Answer

Your { and } aren’t matching up. It’s as if you wanted an else-statement but deleted it and left the closing bracket. Additionally you don’t want to return true if you are doing multiple checks. Once you hit one check that passes, you will submit the form without doing the other checks. See below and let me know if this solves your issue.

<script type="text/javascript" src="/SiteAssets/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery-3.4.1.js"></script>
<script type="text/javascript">

function PreSaveAction() {
    drop1 = $("select[title='Supervisor Completed']").val();
    date1 = $("input[title='Supervisor Date Completed']").val();
    drop2 = $("select[title='IMO Completed']").val();
    date2 = $("input[title='IMO Date Completed']").val();
    drop3 = $("select[title='IAM Completed']").val();
    date3 = $("input[title='IAM Date Completed']").val();

    if (drop1 !=="Select One..." && date1===""){
        alert("The Supervisor Date Completed is a required element. Please Fill this date out.");
        return false;
    }    

    if (drop2 !=="Select One..." && date2===""){
        alert("The IMO Date Completed is a required element. Please Fill this date out.");
        return false;
    }    

    if (drop3 !=="Select One..." && date3===""){
        alert("The IAM Date Completed is a required element. Please Fill this date out.");
        return false;
    }    

    return true;
}



Source: stackoverflow