Pages

Search This Blog

Monday, August 3, 2015

SharePoint TRIM field values before submit

A simple way to implement TRIM on all the Text fields inside SharePoint site is to make use of SharePoint OOTB javascript function named "PreSaveAction()". 

function PreSaveAction()    
{
 return true;
}

This function is called before the data is Added/Updated inside a SharePoint List. This function needs to return a true value so that further events are processed. If you return false from this function, then further execution would be stopped and data won't be saved. Since we are concerned only in replacing the original values with the trimmed ones, we will always return true from this function. 

To do this, just add the below code in a javascript file and link that js file to your master page. Alternatively you can also make use of Content Editor Web part to embed it in a page (if you want it to scope this to a specific list/library) .

function PreSaveAction() {
    try {
        $("input[Type='Text']").each(function () {
            $(this).val($.trim($(this).val()));
        });
        return true;
    }
    catch (e) {
    }
    return true;

}


No comments:

Post a Comment