0

I have a cusotm list inside our sharepoint team site, and inside the list Validation setting i define the following formula:-

=IF(Status="Invoiced",IF([Invoiced Date]<>"",TRUE,FALSE),TRUE)=IF(Status="Delivered",IF([Delivered Date]<>"",TRUE,FALSE),TRUE)

the formula will check if the date fields are entered based on the current status. Our cusotm list have 3 content types, and for all content types they have the above fields (status, Delivered Date & Invoiced Date). but now i need to add an extra validation , as follow:-

  1. if content type = "Personal Form".
  2. and status = "In Progress".
  3. to force user to enter a field named "Date of Birth".

now the "Date of Birth" field will only be inside a single content type named "Personal Form", but seems inside the Validation list's Inset Column select menu, there is not any option to select the content type:- enter image description here

So can anyone advice how i can define my extra validation formula?

1 Answer 1

1

You can overwrite PreSaveAction function on list forms to validate the entered data before saving the data.

In the NewForm.aspx, we need check the ContentTypeID in the URL to know which the content type is used.

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript"> 
function GetParameterValues(param) {  
    var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');  
    for (var i = 0; i < url.length; i++) {  
        var urlparam = url[i].split('=');  
        if (urlparam[0] == param) {  
            return urlparam[1];  
        }  
    }  
}

function PreSaveItem(){
    var result = true;
    //check the content type
    var contentTypeId = GetParameterValues('ContentTypeId');
    //check field values
    var invoicedDate = $('input[title="Invoiced Date"]').val();
    var deliveredDate = $('input[title="Delivered Date"]').val();
    // status is a drop down list
    var status=$("select[title='Status'] option:selected").text();
    if(status == "Invoiced" && invoicedDate == ""){
        alert("please enter Invoiced Date");
        result = false;
    }
    if(status == "Delivered" && deliveredDate == ""){
        alert("please enter Delivered Date");
        result = false;
    }
    if(contentTypeId == "0x01005FBBC90E32AA63449CAC5DE5819EBF2300839177067F7AC647BDDFBB0D03834E7A"){
        var dateOfBirth = $('input[title="Date of Birth"]').val();      
        if(status== "In Progress" && dateOfBirth == ""){
            alert("please enter Date of Birth");
            result = false;
        }   
    }
    return result;
}
</script>

In the EditForm.aspx, check the Content Type choice in the form.

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript"> 
function PreSaveItem(){
    var result = true;
    //check the content type
    var contentType=$("select[title='Content Type'] option:selected").text();
    //check field values
    var invoicedDate = $('input[title="Invoiced Date"]').val();
    var deliveredDate = $('input[title="Delivered Date"]').val();
    // status is a drop down list
    var status=$("select[title='Status'] option:selected").text();
    if(status == "Invoiced" && invoicedDate == ""){
        alert("please enter Invoiced Date");
        result = false;
    }
    if(status == "Delivered" && deliveredDate == ""){
        alert("please enter Delivered Date");
        result = false;
    }
    if(contentType == "cusItem"){ //if the content type of the current item is “cusItem”
        var dateOfBirth = $('input[title="Date of Birth"]').val();  
        if(status== "In Progress" && dateOfBirth == ""){
            alert("please enter Date of Birth");
            result = false;
        }   
    }
    return result;
}
</script>

Note:

Change "0x01005FBBC90E32AA63449CAC5DE5819EBF2300839177067F7AC647BDDFBB0D03834E7A" to your conten type ID. And change the "cusItem" to your content type name "Personal Form".

You can find the content type ID from the URL of the NewForm.aspx when creating item using this content type.

A demo about how to use PreSaveAction function:

https://social.technet.microsoft.com/wiki/contents/articles/31330.sharepoint-using-presaveaction-function-on-custom-list-forms.aspx

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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