4

i want to validate a folder name , it should not start with a number , or any special character, i am not sure about what special symbols are allowed inside folder names , kindly help .

here is my function

function validateFileName() { 
    var re = /[^a-zA-Z0-9\-]/;

      if(!re.test($('#new_folder_name').value)) {
          alert("Error: Input contains invalid characters!");
          $('#new_folder_name').focus();
          return false;
        }

        // validation was successful
        return true;
      }
5
  • What are you validating the folder for? Your server where you know what OS is running and as such can tell what is valid, or a client computer with who knows what OS? Commented Aug 10, 2015 at 10:37
  • You can find info about which characters are not legal to use here support.microsoft.com/en-us/kb/177506 Commented Aug 10, 2015 at 10:37
  • i am creating a new folder and i want a validation on that folder name so that user cannot enter malicious characters while providing name for folder i am not getting how to do it Commented Aug 10, 2015 at 11:22
  • "what special symbols are allowed inside folder names" — It depends on which file system you are using. Commented Aug 10, 2015 at 11:26
  • Name of a folder is based on what file system you use. Each OS has different file system. So, you have to take care of OS, file-system conditions before you validate. Adding to that, file system itself checks if the name is valid or not and you get exceptions for invalid folder name anyway. So, handle the exception and show it to the user. Commented Aug 10, 2015 at 11:36

3 Answers 3

5

As per the Naming Files, path and Namespaces article by MSDN

The following special characters are reserved:

< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)

And the following keywords are also reserved

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. 

Which are case incensitive.

So considering those facts, the possible regex can be:

[^<>:"/\|?*(?:aux|con|nul|prn|com[1-9]|lpt[1-9])]

So your transformed function will be

function validateFileName() { 
    var re = /[^<>:"/\|?*(?:aux|con|nul|prn|com[1-9]|lpt[1-9])]/;

      if(!re.test($('#new_folder_name').value)) {
          alert("Error: Input contains invalid characters!");
          $('#new_folder_name').focus();
          return false;
        }

        // validation was successful
        return true;
      }
Sign up to request clarification or add additional context in comments.

4 Comments

does not works in my case it is taking name with letters and special symbols also which is not allowed as names of any file or folder
Could you give some example file names? i will come up wth a regex
these are not files names these are folder names , for example you use windows as an OS , you can provide folder names as "newfolder" , "new folder" , "new/folder" etc , it can have spaces and , _, -, etc as symbols but we cannot provide names in windows that starts with numbers or some special characters.
/[^<>:"/\|?*(?:aux|con|nul|prn|com[1-9]|lpt[1-9])]/ is invalid
2

Just check for the folder name starts with a letter and does not start with . (Dot)

var re = /^[a-zA-Z].*/;
re.test(folder_name);

This would return true only if the folder name starts with a letter.

4 Comments

it worked for me but not exactly i wanted i want to restrict the folder name started with any special character or number also , and it is not doing this.
How, it only allows a letter to be present at the start.
i am not sure wether it restricts . < , > , : , " , / , \ , | , ? , * these symbols, how to restrict all these while providing name??
ya, above regex won't allow these chars to be present at the start.[a-zA-Z] allows only letters.
2

In C#, there is a function that returns the list of invalid path symbols. Also the Naming Files, Paths, and Namespaces lists some reserved characters that cannot be used in both folder and file names. Here is the regex covering all these restrictions:

var RealInvalidPathChars = /[<>:"\/\\|?*\x00-\x1F]/;

You can test for any of the symbols inside a string, and it will be an invalid path.

Also, there is a list of reserved words that cannot be used as folder names, and we can also use them in a regex:

/^(?:aux|con|clock\$|nul|prn|com[1-9]|lpt[1-9])$/i

If this regex check returns true, the path is invalid.

Here is a snippet combining all these:

$( "#new_folder_name" ).submit(function( event ) {
    var rx = /[<>:"\/\\|?*\x00-\x1F]|^(?:aux|con|clock\$|nul|prn|com[1-9]|lpt[1-9])$/i;
    if(rx.test($( "input:first" ).val())) {
      alert("Error: Input contains invalid characters!");
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="new_folder_name">
  <input type="text" value="<INVALID>">
  <input type="submit" value="Go">
</form>

5 Comments

I am not sure if I get it right: do you also need to validate file name? I thought you only need to validate the folder path. I have just fixed the regex and added the reserved word support.
thank you for the answer but i am only talking about providing names to newly created folders in windows OS . and you are talking about folder path validation
I believe this is the same. I have updated the regex so that it does not allow all reserved characters. If you want to allow any string that can be entered into Create Directory dialog then the rules are a bit different. new/folder will create a folder new with a subfolder folder.Then we need to exclude / from disallowed symbols. You say we cannot provide names in windows that starts with numbers or some special characters, but a folder can start with a digit. Some special characters are those listed in the reserved character list, and they cannot appear anywhere in the name.
i basically use mac os for development i tried making new folder in mac starting with _ and numbers and in between usage of special symbols like ?,>,< , },{ , [ , ] all of them are allowed on mac , so i was not very sure about windows anyways u helped to get me know this :)
Glad to know I could be of help. Please consider accepting and upvoting the answer then. I have tested Directory creation from within C# code now, and it seems my regex catches all problematic cases. Just in case you want to let forcing directories with /, remove it from the character class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.