简体   繁体   中英

how to create regular expression for folder names in javascript?

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;
      }

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;
      }

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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM