In SharePoint 2010, if you need to do some sort of Client side validation using Jquery or JavaScript, then PresaveAction is the one function you are looking for.
In this blog, I will discuss the steps to do a piece of client side validation of a SharePoint List Form. For example NewForm.aspx in any SharePoint List.
PreSaveAction overrides the save buttion in sharepoint form and thereby enabling inserting javascript into it.
In my customer Project, I needed to ensure that , if the Customer Account No is 16 digits and if not I need to provide the alert message saying that you need to enter minimum of 16 digit Account No.
Here is the code snippet below which I had used for implementing the logic.
- function PreSaveItem()
- {
- if ("function"==typeof(PreSaveAction))
- {
- return PreSaveAction();
- }
- return true;
- }
-
-
- function PreSaveAction() {
- return ifValidAccountNo();
- }
-
- function ValidAccountNo()
- {
- if(document.getElementById('customerNo').value.length !=16)
- alert(" Please enter Customer No with 16 digits")
- return false;
-
- }
If you feel that this is helpful, Please comment on the section below. And let me know if you have any suggestions to improve the same.
Happy SharePointing :-)