Validate SharePoint Special Characters on File Upload name - C#

You cannot use the following characters in a document name when you upload the document in SharePoint Site

o   Number sign (#)
o   Tilde (~) 
o   Percent (%)
o   Ampersand (&)
o   Asterisk (*)
o   Braces ({ })
o   Backslash (\)
o   Colon (:)
o   Angle brackets (< >)
o   Question mark (?)
o   Slash (/)
o   Plus sign (+)
o   Pipe (|)
o   Quotation mark (")

Here is the method to check that the documents contains any special characters   
protected bool IsValidFilename(string fileName){
              string pattern = "[\\~#%&*{}/:<>?|\"-]";
              try
              {
                  if (Regex.IsMatch(fileName, pattern))
                  {
                      isSpecialChar = true;
                  }
                  else
                  {
                      isSpecialChar = false;
                  }
              }
              catch (Exception)
              {
                 throw;
              }
              return isSpecialChar;
          }
S    Show a Pop-Up message if the upload file contains any special characters

if (isSpecialChar == true)
{
string script = "<script language='javascript'>alert('" + " ~  # % & * { }  : < > ? / + | are not allowed !" + "')</script>";
Page.ClientScript.RegisterClientScriptBlock(GetType(), "Register", script);
}
 

----------------------------------------------Shashika Hettiarachchi---------------------------------------


Comments