Javascript regex for validating filenames

javascript file extension validation regex
regex for filename and extension
regex filename contains
javascript regex filename without extension
javascript regex test
filename validation in java
regex for image file extensions
regex to find file name

I have a regexp to validate file names. Here is it:

/[0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_ ]+$/

It should allow file names like this:

aaa
aaa.ext
a#
A9#.ext

The following characters are not allowed \ / : * ? \" < > |

The problem is that file names like *.txt or /\kk passes the validation. I am doing validation with keyup event. So when I put one extra character after not allowed one it shows that everything is correct.

You need to add a starting anchor:

/^[0-9a-zA-Z ... ]+$/

This tells the engine to match from the start of the input all the way to the end of the input, whereas for your original expression it only needs to match at the end of the input.

Validate File Name and Extension, RegEx for Matches a string with a valid file name and a valid extension having a length of 3 characters. regex for validating folder name & file name. How to validate an email address in JavaScript. Is there a regular expression to detect a valid regular expression?

For Windows names.

var isValid=(function(){
  var rg1=/^[^\\/:\*\?"<>\|]+$/; // forbidden characters \ / : * ? " < > |
  var rg2=/^\./; // cannot start with dot (.)
  var rg3=/^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; // forbidden file names
  return function isValid(fname){
    return rg1.test(fname)&&!rg2.test(fname)&&!rg3.test(fname);
  }
})();

isValid('file name');

validate filename, Regular Expression to. Regular Expression. Javascript, PCRE. flags. Test String. file name Ok . ext # file name ?/\ bad validate filename. Comments. Post Regex for valid filename in WindowsXP. Ask Question Asked 7 years, 2 months ago. Active 2 years, 9 months ago. Javascript regex for validating filenames.

You need to anchor the expression by using ^ and $. For example:

/^[-\w^&'@{}[\],$=!#().%+~ ]+$/

Note that you need to escape - in a character class, or place it first/last.

File Name & File Extensions, I found a regex for valid filenames from your reference, and put it in a validity checking function, as below. Since components can be Saved  Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET

/^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|\*\?\\:<>/$"]*[^\.\|\*\?\\:<>/$"]+$/

Must not be empty.
Must not start with .
Must not be com0-com9, con, lpt0-lpt9, nul, prn
Must not contain | * ? \ : < > $
Must not end with .

Javascript regex to validate component and group name characters , I've been looking everywhere for a regex to validate a filename. Obviously this would disallow: \ / : * ? | Someone must have this in their arsenal. Regular Expression to Validate file name & Extesions. Character classes. any character except newline \w \d \s: word, digit, whitespace

I would try something with this Regex (you can even make a validation attribute for ASP.NET MVC with it!):

@"^[^\u0022\u003C\u003E\u007C\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u003A\u002A\u003F\u005C\u002F]*$"

If it matches the input, it is valid filename (at least on Windows).

regex for filename validation - Javascript, Ok here is a quick regular expression to check if a file name only contains Regular Expression – Javascript- To check filename to see if alpha numeric characters Regular Expressions - validating the EmailIn "Javascript". What might be valid file names on a local system, might not be valid file names in all the systems it is supposed to be represented, e.g. database columns in VARCHAR, URLs etc. So not having to tackle it later, more restrictive naming at first, might be a go to strategy. – Johny Skovdal Jan 4 '17 at 21:28

Regular Expression – Javascript- To check filename to see if alpha , I am trying to weed out 'bad' characters when a user is creating a filename. I am doing so with Javascript. Below is my code, another set of eyes would be great. For anyone coming from DuckDuckGo like me (yes, you shouldn't use Google :p), this regex tests for valid filenames and file paths on Windows, Unix and macOS: Note: On Windows, \ is not allowed in filenames, but the above regex works for checking valid paths on Windows.

Javascript Regex filename validation Solutions, I have a regexp to validate file names. Here is it: /[0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\​(\)\.\%\+\~\_ ]+$/. It should allow file names like this: aaa aaa.ext a# A9#.ext. I simply want to validate the filename of the image being uploaded to ensure that it does not have spaces or unusual characters. This is my latest attempt from searching around, still no luck. Cou

Javascript regex for validating filenames, Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP Recently, I was looking for a regular expression to validate a file path and extension This only allows upper/lower A-Z, 0-9, underscore and hyphen in the file name- no spaces allowed, but you can add them if needed  I need to validate the file name of each file before it is added to the upload queue, however I can't find an event for this, so I have tried with the uploadFileCountChange event, but this event happens AFTER the files have been added to the queue.

Comments
  • the accepted answer fixed your regex, but your regex is not correct for checking Windows filenames. see here
  • Escape / with \ in rg1. At least, VS 2010 thinks so.
  • ^(?!\.)(?!com[0-9]$)(?!con$)(?!lpt[0-9]$)(?!nul$)(?!prn$)[^\|*\?\\:<>/$"]*[^\.\|*\?\\:<>/$"]+$
  • Thanks @RyanWilliams for comment. But I know way to create file in Windows with long file name that contains dot in the end of file name and without extention :)
  • It should be return !rg1.test(fname) && !rg2.test(fname) && !rg3.test(fname); I guess.
  • AUX is missing, COM0 is actually valid, starting with "." is actually valid... ref
  • Can you tell a bit what this regex does?
  • / (forward slash) is missing, AUX is missing, COM0 is actually valid, starting with "." is actually valid.... ref
  • shortened: /^(con|prn|aux|nul|((com|lpt)[0-9]))$|([<>:"\/\\|?*])|(\.|\s)$/ig