Content Script Editor - Javascript Snippet

Iron Contributor

I am experimenting in an office 365 trial tenant and have a requirement to show a popup message based on the value of a field in my EditForm. I am trying the instructions listed on https://www.dmcinfo.com/latest-thinking/blog/id/9279/sharepoint-form-validation-using-javascript-jqu...

 

It works fine the way it is and shows the error message if the field is left blank etc. But If I add a small code, as underlined below, then the nothing works and not even simple alert messages.

 

Truly appreciate any guidance.

 

<script src="//code.jquery.com/jquery-latest.min.js"></script>	
<script>	
	function PreSaveItem() {
	    a: ;return "function" == typeof PreSaveAction ? PreSaveAction() : true
	}
</script>	
	
<script>	
//When the user clicks 'Save', SharePoint will run this function	
function PreSaveAction(){	
    //Returning true will cause the form to be submitted, false will prevent form submission	
    var result = true;	
    alert("Test");     	
    //Clear out existing error messages	
    $('.errorMessage').remove();	
     	
    //Select the input of a text field	
    var textField = GetFieldByDisplayName("Title");	
    var column1Field = GetFieldByDisplayName("Column 1");	
    alert (textField.val().trim());

    If !(textField.val().trim()) {
       alert(textField.val().trim())
       } else {
      alert("Else Part")
     };
    
    //Check if the input is empty	
    if (textField && !textField.val().trim() {	
        result = false;	
        //Write a message indicating to the user that the field is empty	
        WriteErrorMessage(textField, "You ccan''t leave this blank.");         // Intentionally made double c in ccan't to see if this message is coming from our code and not the system code.	
    }	
     	
    //Select the textarea of a multi-line text field	
    var multiLineTextField = GetFieldByDisplayName("Example Multi Line Text Field");	
 	
    return result;	
}	
	
//Append an error message to a field       	
function WriteErrorMessage(inputElement, message){	
    var errorMessageMarkup = '<span class="errorMessage ms-formvalidation ms-csrformvalidation"><span role="alert">' + message + '<br></span>';	
    $(inputElement).parent().append(errorMessageMarkup);	
}	
	
//Gets a jQuery object that represents a field element	
function GetFieldByDisplayName(fieldName){	
    var field = $('input[title="' + fieldName + '"], [title="' + fieldName + ' Required Field"]');	
    if (!field){	
        field = $('textarea[title="' + fieldName + '"], [title="' + fieldName + ' Required Field"]');	
    }	
    return field;	
}	
</script>
</span>

 

4 Replies
Since you are on the classic view of a SPO list: why not using JSLINK instead? Also bear in mind that both options will not work in the modern UI

Thanks Juan. Idea was to use JSLINK eventually after I have tested my code, since it didn't work with snippet I assume it wouldn't with JSLINK as well.

But even using JSLINK, if it's not going to work in modern and classic sure will go out of support at some point, so either way I am setting myself up for failure in future.

 

So this is another function which Microsoft is going away with in Office-365? 

 

 

 

Well, I would say that depending on the list experience (classic or modern) you have to use different techniques...for modern UI the only technique you have today to apply customizations to lists is by means of SPFx Extensions

Thanks Juan. I think Microsoft is trying to avoid adding custom Javascript for the security reasons and instead providing the limited controls (to start with) by allowing customizing using the "PowerApps". But there also we can add custom conditions and actions in native JavaScript. Anyways, I will give up and take a back seat at this point owing to the fact of me not knowing the web technologies and respective security implications etc.

 

But I wish I knew any my snippet is not working before I give up.

 

Also when I created a team site in Office-365 and then created a list under it, and assigned this list the "modern experience". Now when I go to list it shows in modern view showing the top navigation bar with a down arrow etc, but when I go back to the team site landing page it switches the top nagivation bar. Is there a way I could have a consistent "modern experience" within list and outside list (i.e. main page) ?

 

Thanks a lot.