tags:

views:

194

answers:

3

Seems there are some problems using asp.net regular expression validators where they work in firefox but not in some flavors of i.e. (and maybe vice-versa, I don't know).

Anyway, anyone have a replacement for this:

([a-zA-Z1-9]*)\.(((P|p)(D|d)(F|f))|((d|D)(o|O)(c|C)))

To basically match any filename/path with a PDF or Doc extension?

As I said, this works fine when run under firefox, but not i.e. 7

EDIT: I am talking about client-side validation here.

+2  A: 

Your expression is not very lenient:

 ([a-zA-Z1-9]*)

Would not match MyPDF-0.pdf or, more importantly, C:\Path\To\Doc.pdf. Check the form input, see if you have a full file path or just a filename.

Edit:

Try this:

\.([Pp][Dd][Ff]|[Dd][Oo][Cc][Xx]?)$

Unless you can make it case insensitive, like in JavaScript:

/\.(pdf|docx?)$/i
James Socol
A: 
<script type="text/javascript">
var re = /([a-zA-Z1-9]*)\.(((P|p)(D|d)(F|f))|((d|D)(o|O)(c|C)))/;
var filename = "abcd.PdF";
document.writeln(re.test(filename)); // true in IE 7
</script>

Can you provide a test case that does not work in IE 7 but does work in Firefox?

You could replace all those ors with /([a-zA-Z1-9]*)\.(([Pp][Dd][Ff])|([dD][oO][cC]))/ or /([a-z1-9]*)\.((pdf)|(doc))/i.

James has a good point, if this is supposed to match the value in an <input type="file">, Internet Explorer may include the path while Firefox does not (I know it does on the server, it may on the client as well).

Good advice would be to actually check the value you are trying to match against your regular expression to ensure it is what you think it is.

Grant Wagner
A: 

There is nothing wrong with your regular expression (apart from some optimizations you can make as suggested by others). The following code works fine in IE 7 and Firefox and successfully matches the string:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript">
    window.onload = function() {
        var regex = /([a-zA-Z1-9]*)\.(((P|p)(D|d)(F|f))|((d|D)(o|O)(c|C)))/;
     alert(regex.test('test.pdf'));
    };
    </script>
</head>
<body></body>
</html>

So there's something else that might cause the issue. I would suggest you to try to isolate the problem as much as possible and the solution will then be easier to find.

Darin Dimitrov