0

I need to read file name from a text box which is given by the user for creating file in windows. So I need to restrict the special characters to be entered in that text box \ / : * ? " < > |

How I can validate in javascript function using regular expression? I tried the below code.

var rg1 = new RegExp('/^[^\\/:\*\?"<>\|]+$/');
if (rg1.test($("qcAttachmentName").value)) {
    showErrorDetails("Enter a valid Attachment Name", 0);
    return "";
}

But it is not working.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Hanumantha
  • 107
  • 2
  • 12
  • Please post what you've tried so far. People will be more inclined to help you that way than if you expect a full solution without effort on your part. – Amos M. Carpenter May 15 '15 at 05:05
  • possible duplicate of [Javascript regex for validating filenames](http://stackoverflow.com/questions/11100821/javascript-regex-for-validating-filenames) – Amal Dev May 15 '15 at 05:11
  • @Amal Dev , That post is not helping me. – Hanumantha May 15 '15 at 05:14
  • the accepted answer fixes your regex, but your regex is not correct for checking filenames under Windows, [see here](https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file) – Leon Aug 09 '18 at 10:14

1 Answers1

1

Your script and regex pattern does have some errors. Please find the fiddle here for the working sample

Amal Dev
  • 1,938
  • 1
  • 14
  • 26