I have been requested by my manager to create a complete button which would perform the validation of the edit form and then save the item in the list. I know this can be done through a save button but they need save button to save the form even if the data is not completely filled.
All my validation are done but i am not aware as to how to create a button. I have restricted access so i have to create a button through content editor or a script editor.
Thank You.
2 Answers
Step 1:
You can get the values from the default SharePoint form using Javascript. Use any browser debugger tool to identify the ID of the elements(form fields like - input, choice, etc.) whose data you need. This below screenshot explains the same.
Step 2:
Get the value of the particular element using Javascript-
document.getElementById("<<myIDfoundinStep1>>").value
Step 3:
You can use HTML button for this purpose. You can create a button in your content/script editor using this -
<button type="button" onclick="myCompleteFunction()">Complete</button>
myCompleteFunction() will hold the method for saving the data. You can use REST API, etc for saving data. Here is a link that can help - link
Hope this helps!
-
i am aware of how to create a button through html, but i am finding it difficult as to how to create a button to save the data in the list and perform the validation. I need the code for it honestly because i have not worked on it. Apr 16, 2018 at 9:46
-
I see... do you have any HTML forms where you are entering data and then trying to save? Or are you using default SharePoint list form? Please answer the same and then I could help you with the code and methodology. Apr 16, 2018 at 10:04
-
i am using default share point form. Sorry for the late reply Apr 19, 2018 at 4:16
-
There you go @jayantkhushalani I have updated the answer above. Please mark it as an answer if it resolves your issue. If you are still facing trouble, let me know Apr 19, 2018 at 13:34
-
Where do I add the validation code in your above answer. In my complete function()? Apr 19, 2018 at 14:32
There's code on the net which will rename the 'save' button if you want it to say complete.
I would embed a javascript with Function PreSaveAction() {
then do your validation. if validation fails, then just "return false;" in the function. They'll only be able to save the record if you 'return true;'
Yoou can use javascript alerts to tell the precise reason the validation failed.
The other approach is to use 'item validation' (not column validation). this allows you to access most fields in the validation - It is constructed as a big IF statement which returns true or false. I don't like approach as it 1) tells the user there's a problem at the bottom of the form - they have to scroll down to see it and 2) you can't really provide any information on WHY it failed.
You also cannot access certain fields.
There's tons of examples of using Javascript to access the values of the fields as they are currently displayed but SPServices is helpful to -
-
i do know that approach which you are suggesting and thank you for it. But i do not require that, all i require is a separate complete button than renaming a save button. Thanks anyways and the validation through java script has been done already we just need a complete button to bind it to rather than a save button as the form can be saved in midway with some fields left blank and thus save button comes in handy. Apr 19, 2018 at 4:18