2

By default SharePoint does not allow the following special characters to be present in attachment file names

~, #, %, & , *, {, }, \, :, <, >, ?, /, |, “

How can I validate that any attachments file name does not have ANY special characters(not just the ones above, but all special characters) in JavaScript?

1

Check below:How To Validate File Names Of Attachments On SharePoint Forms

How could we validate attachment file names and prohibit attachments having invalid names? We can use the same method as described in my former post. We should update our onAttachOKbuttonClicked method with the file name validations. The new version, including the duplicate check as well as file name length and special character validation:

Source:

$(document).ready(attachEventHandlers);

function attachEventHandlers() {
    // override the default event handler with our custom method
    $('#attachOKbutton').attr("onclick", "onAttachOKbuttonClicked()");
}

function onAttachOKbuttonClicked() {
    var newFilePath = $('#attachmentsOnClient').find('input').last().val();
    // get the file name from the file path as described at
    // http://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript
    // TrimWhiteSpaces is a js method of SharePoint to filter out special characters from the file name
    var newFileName = TrimWhiteSpaces(newFilePath).replace(/^.*[\\\/]/, '');

    var maxFileNameLength = 128;

    // Information about the characters that you cannot use in site names, folder names, and file names in SharePoint
    // http://support.microsoft.com/kb/905231
    // http://www.sysadminsblog.com/microsoft/file-name-length-and-character-restrictions-for-sharepoint/
    // ivalid characters in SP:
    // ~#%&*{}\:<>?/+|"
    // ivalid characters in file system:
    // \/:*?"<>|
    // we have to check only:
    // ~#%&{}+|
    /*
    Cant be longer than 128 characters
    Cant use: ~ # % & * { } \ : < > ? / + | "; RegExp: [~#%\&{}+\|] – do not include characters that are not allowed in the file system
    Cant use the period character consecutively in the middle of a file name (blahblah.docx); RegExp: \\.\\.
    Cant use the period character at the end of a file name; RegExp:  ^\\.
    Cant use the period character at the start of a file name; RegExp:  \\.$    
    */

    var match = (new RegExp('[~#%\&{}+\|]|\\.\\.|^\\.|\\.$')).test(newFileName);
    if (match) {
        alert("Ivalid file name. The name of the attached file contains invalid characters.");
    }
    else if (newFileName.length > maxFileNameLength) {
        alert("Ivalid file name. The name of the attached file is too long.");
    }
    else {
        // it is the same duplicate check code from former post (https://pholpar.wordpress.com/2014/03/12/how-to-check-for-duplicated-attachments-on-sharepoint-forms/)
        var foundDuplication = false;

        $('#idAttachmentsTable').find('tbody').find('tr').each(function () {
            var existingFileName = $(this).find('.ms-vb').find('a').text();
            // if the existingFileName is empty then the attachment was uploaded in this session
            // that is, it is not saved yet
            if (existingFileName == '') {
                var existingFilePath = $(this).find('.ms-vb').find('span').text();
                existingFileName = existingFilePath.replace(/^.*[\\\/]/, '');
            }

            if (newFileName == existingFileName) {
                foundDuplication = true;
                return false;
            }
        });

        if (foundDuplication) {
            alert("A file with name '" + newFileName + "' is already attached to this item.");
        }
        else {
            // call the OkAttach js method of SharePoint
            // this is the method that is originally called by uploading attachments
            OkAttach();
        }

    }
}

Also check this one Sharepoint List item attachments validating

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.