Sijo
Sijo

Reputation: 33

Regexp to match file name without the path

I am using this code to find blank spaces in a file name in a SharePoint list attachment.

function PreSaveAction() {
    var attachment;
    var filename = "";
    var fileNameSpecialCharacters = new RegExp('\\s', 'g');
    try {
        attachment = document.getElementById("idAttachmentsTable").getElementsByTagName("span")[0].firstChild;
        filename = attachment.data;
    } catch (e) {}
    if (fileNameSpecialCharacters.test(filename)) {
        alert("Please remove the special characters and white spaces from file attachment name.");
        return false;
    } else {
        return true;
    }
}

In IE it checks white space in the 'whole path' instead of the file name. In all other browsers it works fine,

Can anyone help me to make this code check the file name only, not the whole path?

Upvotes: 0

Views: 482

Answers (3)

luastoned
luastoned

Reputation: 663

Taken from what stema said, this will check both / and \

\s(?=[^/\\]*$)

Upvotes: 1

stema
stema

Reputation: 92986

You could do something like this

\s(?=[^\\]*$)

That means find whitespace but only if there is no \ ahead till the end of string.

See it here on Regexr

But of course this will work only if the character that divides the directories is the \. Maybe JavaScript can get this character from the OS somehow (I don't know JavaScript in that depth)?

Upvotes: 0

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

Split off the file name from the path name. Then test the file name only.

Upvotes: 2

Related Questions