als-filename-validator
is a Node.js module for validating file and directory names across different operating systems including Windows, Linux, and MacOS.
- string validation for filename parameter (return false)
- validation for empty string (return false)
Use npm to install the package:
npm install als-filename-validator
The module exports a function that checks if a given file name is valid for a specified operating system.
const isValidFileName = require('als-filename-validator');
const fileName = "test.txt";
const os = "windows"; // can be 'windows', 'linux', 'mac', 'darwin', 'win32', or 'all'
if (isValidFileName(fileName, os)) {
console.log("The file name is valid.");
} else {
console.log("The file name is not valid.");
}
The second argument os is optional. If not provided, the function will perform validation checks for all operating systems ('all').
You can also explicitly check for all operating systems by passing 'all' as the second argument.
In addition to Node.js environments, the als-filename-validator function is designed to be compatible with browser environments. This makes it suitable for a wide range of JavaScript applications, including those running on the client-side in web browsers.
To use it in a browser, simply include the script in your HTML and access the isValidFileName function globally.
Example of including the script in HTML:
<script src="node_modules/als-filename-validator/index.js"></script>
The following table provides some examples of file names and the function's output:
File Name | OS | Is Valid? |
---|---|---|
test.txt | all | Yes |
.test | all | No |
test:.txt | windows | No |
my*file.txt | linux | No |
prn.txt | windows | No |
valid_file_name | mac | Yes |
space_in_end.txt | all | No |
CON | windows | No |
normal_file_name.jpeg | all | Yes |
:invalid:file:name.txt | mac | No |