Rename item in order to work when you create a new item based on the title

Quite a title this time 🙂
Let me explain:

I have a list where people can add items to: requests for a ticket.
Those tickets are ( if approved ) getting into a workflow: every ticket becomes a folder in a list, where we can have actions as list items.

The problem was that my workflow crashes when a forbidden character is used as the name for the new item(item1 “Title” serves as item2 “Name”)

The forbidden SharePoint folder and file name rules are:
o Do not use: ” # % & * : < > ? \ / { | } ~
o File names cannot be longer than 128 characters
o Do not use the period character consecutively in the middle of a file name.
o You cannot use the period character at the end of a file name
o You cannot start a file name with the period character

This code checks for forbidden characters (# % & * : < > ? \ / { | } ~) and removes them on Keyup().
It also checks if the title has more then 128 characters.

I know it is not complete (no check on period as first char and so on), so please bare with me.

function noweirdchars(){
        var str = $(":select[title=Title]").val();
                var patt = new RegExp( /[#%&*:<>?\/\\{|}~]/m );
                $(":select[title=Title]").val(str.replace(patt,""));
                                          
if ($(":select[title=Title]").val().length > 128)
        {
                alert("Name can only have 128 characters");   
                event.preventDefault(); 
        } 
}
$(document).ready(function() {
                $(":input[title='Title']").keyup(function(){
                noweirdchars(); 
        });
});

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.