espresso_coffee
espresso_coffee

Reputation: 6110

Regular expression works in Firefox but not in Chrome?

I have regular expression that checks for the file name. So far works fine in Firefox but when I tested in Chrome my regular expression was triggering if statement with the file that is properly named. Here is my regex:

if(!/^[a-z0-9_.@() -]+\.[^.]+$/i.test(fileName)){
    alert("Your file name has an invalid character.");
}

Alert should show up only if file has invalid name. Like I said this file my test.txt works in Firefox but in chrome alert was triggered. If anyone know why this would happen please let me know. Thanks.

Upvotes: 3

Views: 171

Answers (1)

espresso_coffee
espresso_coffee

Reputation: 6110

So I found solution for my problem. I had to remove everything but the file name. I used this code:

var fileName = $('#fileUpload').val().split('\\').pop();

This will produce: my test.txt, if I did not use .split('\\').pop() my file anme would look like this: C:\fakepath\my test.txt

Upvotes: 1

Related Questions