7

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
  • 365
  • 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,643
  • 1
  • 21
  • 24
7

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

Alex Aza
  • 70,453
  • 24
  • 147
  • 129
3
new RegExp('^[a-zA-Zа-яА-Я0-9_!]+$')

http://regexpal.com/ try this site

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

its work for me. Basically it takes:

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