Hi there.
I am trying to do a simple validation on the Sharepoint EditForm.ASPX file to check and see if a number of list items are populated with text when the status of the issue is set to closed. I have reviewed a number of websites, forms, and books in relation to this and everything points to simply putting a javascript into the ASPX with PreSaveAction() in it. I copied and pasted out text and have tried this, but I can never get the script to run.
Here is the sample simple code from the EditForm.ASPX file to get started with:
<script type="text/javascript"> function PreSaveAction() { alert('In save funtion'); var containmentText = getValueWithFieldName('input', 'Containment', 'ff13'); var checkSelectVal = getTagFromIdentifierAndTitle("select","","Status"); if (checkSelectVal.value == 'Closed' && containmentText.value == '') { alert('Incomplete related Task found, please close all related Tasks and try again.'); return false; // Cancel the item save process } return true; // OK to proceed with the save item } } // getValueWithFieldName: Get a form field value using its tagName, fieldname, and identifier to find it in the page // Arguments: // tagName: The type of input field (input, select, etc.) // fieldName: The name of the list column // identifier: The identifier for the instance of the fieldName (ff1, ff2, etc.) // function getValueWithFieldName(tagName, fieldName, identifier) { //alert('getValueWithFieldName: tagName=' + tagName + ' fieldname=' + fieldName + ' identifier' + identifier); var theInput = getTagFromIdentifierAndFieldname(tagName, fieldName, identifier); //alert('getValueWithFieldName: theInput.id=' + theInput.id + ' theInput.value=' + theInput.value); return theInput.value; } // getTagFromIdentifierAndFieldname: Get an input field's tag object using its tagName, fieldname, and identifier to find it in the page // Arguments: // tagName: The type of input field (input, select, etc.) // fieldName: The name of the list column // identifier: The identifier for the instance of the fieldName (ff1, ff2, etc.) // function getTagFromIdentifierAndFieldname(tagName, fieldName, identifier) { //alert('getTagFromIdentifierAndTitle: tagName=' + tagName + ' fieldname=' + fieldName + ' identifier' + identifier); var len = identifier.length; var tags = document.getElementsByTagName(tagName); for (var i=0; i < tags.length; i++) { if (tags[i].title == fieldName) { //alert('HIT tags[' + i + '].title' + tags[i].title + ' tags[' + i + '].id=' + tags[i].id + ' identifier=' + identifier); if((tags[i].id == identifier) || (tags[i].id.indexOf(identifier) > 0)) { return tags[i]; } </script>
I know that this script works as I can put a _spBodyOnLoadFunctionNames funtion call in there instead and that works and I get get an alert to show on the page load.
The button is the standard default button in Sharepoiint. The script for it is as follows:
<tr> <td class="ms-toolbar" nowrap=""> <table> <tr> <td width="99%" class="ms-toolbar" nowrap=""><IMG SRC="/_layouts/images/blank.gif" width="1" height="18"/></td> <td class="ms-toolbar" nowrap=""> <SharePoint:SaveButton runat="server" ControlMode="Edit" id="savebutton1"/> </td> <td class="ms-separator"> </td> <td class="ms-toolbar" nowrap="" align="right"> <SharePoint:GoBackButton runat="server" ControlMode="Edit" id="gobackbutton1"/> </td> </tr> </table> </td> </tr>
So SaveButton is sent as a command in the aSPX so I gather that PreSaveAction is triggered by that.
I tried adding a Content Editor Web Part to the page too, but that ends up only showing the fuction as text on the page.
Any help or assistance on this would be appreciated. Using Sharepoint 2007 Developer for this.
Regard
Morgan
Regards mBardon