Solution to "TypeError: m[5] is undefined", "TypeError: Cannot read property length of undefined" and How to Override JavaScript in ASP.NET WebResource.axd files

You’ll sometimes get this error in Firefox and Chrome, but not IE.

“TypeError: m[5] is undefined”

“TypeError: Cannot read property ‘length’ of undefined”

There has been a known issue with Firefox and Chrome when you combine RegularExpressionValidators and RequiredFieldValidators. It’s been around since 2009, but no one has addressed the issue. Here’s the bug report on ASP.NET forums: http://forums.asp.net/t/1417973.aspx and here’s the bug report filed to Mozilla for the FF issue. Neither of them have a solution.

I narrowed it down to these lines of code in WebResource.axd – the one with the Validator control JavaScript that starts with

var Page_ValidationVer = “125”;

if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
  year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))

The root of the problem is that the WebResource JavaScript isn’t checking if “m[2]” and “m[5]” are undefined before checking the “length” property. The obvious fix is to do just that, but it’s embedded in an automatically generated WebResource.axd file. You can’t edit it directly, but since Microsoft didn’t scope the JavaScript properly you can override that method if you insert a method with the same name at the bottom of the page.

Here’s how I resolved it:

1. Create a new .js file called WebResourceOverrides.js

2. Copy the entire “ValidatorConvert” method from the WebResource.axd file to the file I created in Step 1.

3. Modify the method to check for undefined

// added m[2] undefined check because it was failing in FF and Chrome
if (m != null && ( (typeof(m[2]) != 'undefined' && m[2].length == 4) || val.dateorder == "ymd")) {

.
.
.

// added m[5] undefined check because it was failing in FF and Chrome
year = (typeof m[5] != 'undefined' && m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))

4. Reference that new js file AT THE BOTTOM OF my master page

<script type="text/javascript" src="/WebResourceOverrides.js"</script>

You can download my version of the WebResourceOverrides.js file from here: http://jongallant.com/blogassets/WebResourceOverrides.js.txt. Just remove the “.txt” extension, copy to your site, add the reference to the bottom of your master page and you are good to go.

Jon