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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With