Skip to content

Instantly share code, notes, and snippets.

@charitygrace
Created July 27, 2015 19:24
Show Gist options
  • Select an option

  • Save charitygrace/67a8caf91420ca18dabc to your computer and use it in GitHub Desktop.

Select an option

Save charitygrace/67a8caf91420ca18dabc to your computer and use it in GitHub Desktop.
(?:^|\/|\\)(?:[a-zA-Z0-9\s\-\_])*\.[a-zA-Z]*$
This will check just the filename in a file upload and ignore the file path before the file name.
This will allow any filename with letters, numbers, dashes, underscores and spaces.
Use in jquery validator like this:
$.validator.addMethod("fileName", function(value, element) {
return this.optional(element) || /(?:^|\/|\\)(?:[a-zA-Z0-9\s\-\_])*\.[a-zA-Z]*$/.test(value);
}, "File names can only contain letters, numbers, dashes, and underscores (and spaces).");
This post was helpful in determining how to ignore the file path before the file name: https://forum.jquery.com/topic/validate-a-filename-with-the-validation-pluging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment