scoohh
scoohh

Reputation: 375

regex for validating folder name & file name

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.

Upvotes: 8

Views: 21489

Answers (4)

Rousonur Jaman
Rousonur Jaman

Reputation: 1271

new RegExp('^[^\\\\\/\?\%\*\:\|\"<>]+$')

its work for me. Basically it takes:

new RegExp('^[^\\/?%*:|"<>]+$')

Upvotes: 1

Anja Ishmukhametova
Anja Ishmukhametova

Reputation: 1564

new RegExp('^[a-zA-Zа-яА-Я0-9_!]+$')

http://regexpal.com/ try this site

Upvotes: 3

King Skippus
King Skippus

Reputation: 3826

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. */
}

Upvotes: 14

Alex Aza
Alex Aza

Reputation: 78487

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

Upvotes: 8

Related Questions