Forum Discussion
Vivek Jain
Jan 19, 2018Iron Contributor
Content Script Editor - Javascript Snippet
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-jquery
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>
- 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
- Vivek JainIron Contributor
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