8

I want to validate a file name

Name of file or folder should not contain \ / ? % * : | " < > .

Could you please suggest me the regex expression to use in preg_match()?

Thanks.

scoohh
  • 375
  • 6
  • 12
  • 19

4 Answers4

14

It would be more efficient to use the strpbrk() function.

if (strpbrk($filename, "\\/?%*:|\"<>") === FALSE) {
  /* $filename is legal; doesn't contain illegal character. */
}
else {
  /* $filename contains at least one illegal character. */
}
King Skippus
  • 3,801
  • 1
  • 24
  • 24
8

The regex that meets your requirements: ^[^\\/?%*:|"<>\.]+$

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
3
new RegExp('^[a-zA-Zа-яА-Я0-9_!]+$')

http://regexpal.com/ try this site

Anja Ishmukhametova
  • 1,535
  • 16
  • 14
1
new RegExp('^[^\\\\\/\?\%\*\:\|\"<>]+$')

its work for me. Basically it takes:

new RegExp('^[^\\/?%*:|"<>]+$')
Rousonur Jaman
  • 1,183
  • 14
  • 19