0

I am having a parent list and child list.

Parent list (Contract) is having an Contract Num, Contract Amount, Start Date and End Date columns.

Child list (Project) is having and Project Name, Parent Name, Amount, Start Date and End Date columns.

    List Name: Contract
    Contract Num    Start Date  End Date    Contract Amount
    CT2014-001      8/1/2014    8/30/2015   $400.00
    CT2014-002      8/1/2014    8/30/2015   $600.00
    CT2014-003      8/1/2014    8/30/2015   $250.00

    List Name: Project
         Project Name   Contract Num  Start Date    End Date    Project Amount
 Valid   PT2014-001     CT2014-001    8/5/2014      8/20/2014   $100
 Invalid PT2014-002     CT2014-002    9/1/2014      10/1/2014   $300
 Invalid PT2014-002     CT2014-002    8/1/2014      8/15/2014   $3000

    JAVASCRIPT VALIDATION           
    When a new project is added, the goal is to validate the inputs for
    1.  Start Date must be greater than or equal to contract start date
    2.  End Date must be less than or equal to the contract end date
    3.  Project Amount must be less than or equal to contract amount

Please provide solution and let me know if I can provide any additional information.

Thanks in advance.

1

In newform.aspx and editform.aspx of a SharePoint list item, a javascript function "PreSaveAction()" is called when the save button is clicked. By default, this function returns true, which enables SharePoint to Save/Update the item. You can override this function to provide your own validation. Within PreSaveAction(), you'll need to use AJAX to contact the lists.asmx SharePoint Web Service. to get the information you need from the Parent List. You'll want to add jQuery and some JavaScript to newform.aspx and editform.aspx in your Project list.

Here is some example code based on code I've used previously to accomplish similar validations. For help with creating the CAML query and reading the results, I use SharePoint CAML Query Helper for 2007, 2010, and 2013.

function PreSaveAction() {
    var arrayErrorMessages = [];
    var contractNum = $("input[title='Contract Num']").val();
    var projectStartDate = $("input[title='Start Date']").val();
    var projectEndDate = $("input[title='End Date']").val();
    var projectAmount = $("input[title='Project Amount']").val();
    var targetUrl = "/_vti_bin/lists.asmx";
    var listName = "Project";
    var soapEnvArray = [];
    soapEnvArray.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    soapEnvArray.push("<soap:Envelope ");
    soapEnvArray.push("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
    soapEnvArray.push("<soap:Body><GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"><listName>" + listName + "</listName>");
    soapEnvArray.push("<viewName></viewName><query><Query>");
    soapEnvArray.push("<Where>");
    soapEnvArray.push("<Eq>");
    soapEnvArray.push("<FieldRef Name=\"Contract_x0020_Num\" />");
    soapEnvArray.push("<Value Type=\"Text\">" + contractNum + "</Value>");
    soapEnvArray.push("</Eq>");
    soapEnvArray.push("</Where>");
    soapEnvArray.push("</Query></query>");
    soapEnvArray.push("<viewFields>");
    soapEnvArray.push("<ViewFields>");
    soapEnvArray.push("<FieldRef Name=\"Contract_x0020_Num\" />");
    soapEnvArray.push("<FieldRef Name=\"Start_x0020_Date\" />");
    soapEnvArray.push("<FieldRef Name=\"End_x0020_Date\" />");
    soapEnvArray.push("<FieldRef Name=\"Project_x0020_Amount\" />");
    soapEnvArray.push("</ViewFields>");
    soapEnvArray.push("</viewFields>");
    soapEnvArray.push("<rowLimit>0</rowLimit><queryOptions><QueryOptions xmlns=\"\">");
    soapEnvArray.push("<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>");
    soapEnvArray.push("<ViewAttributes Scope = \"RecursiveAll\"/>");
    soapEnvArray.push("</QueryOptions></queryOptions>");
    soapEnvArray.push("</GetListItems></soap:Body></soap:Envelope>");
    var soapEnv = soapEnvArray.join("");
    $.ajax({
        cache: false,
        url: targetUrl,
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        contentType: "text/xml; charset=utf-8",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
        },
        complete: function(msg) {
            if (msg.status === 200) {
                //AJAX Success
                var totalTaskCount = $(msg.responseXML).find("z\\:row, row").length;
                var completedTaskCount = 0;
                if (totalTaskCount > 0) {
                    $(msg.responseXML).find("z\\:row, row").each(function() {
                        var contractStartDate = $(this).attr("ows_Start_x0020_Date");
                        var contractEndDate = $(this).attr("ows_End_x0020_Date");
                        var contractAmount = $(this).attr("ows_Contract_x0020_Amount");
                        if (contractAmount > projectAmount) {
                            arrayErrorMessages.push("Project Amount must be less than or equal to contract amount.");
                        }
                        //Do more validation
                    });
                }
            } else {
                //AJAX Failure
                var errorCode = $(msg.responseXML).find("errorcode").text();
                var errorString = $(msg.responseXML).find("errorstring").text();
                if (errorString.length === 0) {
                    errorString = $(msg.responseXML).find("faultstring").text();
                }
                errorString = errorString.replace(/(\r\n|\n|\r)/gm, "");
                showStatusBar("Oh no! " + errorString + "(" + errorCode + ")");
            }
        },
    });
    if (arrayErrorMessages.length > 0) {
        var errorMessage = "";
        for (var e = 0; e < arrayErrorMessages.length; e++) {
            showStatusBar(arrayErrorMessages[e]);
        }
        return false;
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.