PreSaveAction in SharePoint 2010

PreSaveAction in SharePoint 2010

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.
  1. function PreSaveItem()  
  2. {     
  3.    if ("function"==typeof(PreSaveAction))  
  4.    {  
  5.       return PreSaveAction();  
  6.    }  
  7.    return true;  
  8. }  
  9.   
  10.   
  11. function PreSaveAction() {     
  12.    return ifValidAccountNo();  
  13. }  
  14.   
  15. function ValidAccountNo()  
  16. {  
  17.    if(document.getElementById('customerNo').value.length !=16)  
  18.   alert(" Please enter Customer No with 16 digits")  
  19.   return false;  
  20.   
  21. }  
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 :-)