jQuery.Validate plugin $.format function null error

jQuery.Validate is an awesome plugin.. Albeit with a few minor issues.

This one happens when using the $.format function which is part of jquery.validate 1.7 and previous versions too.

When you use the $.format like this:

$.format(“Hello {0} ”, userName)

if the userName variable is null then this will be the outcome: “Hello null”

To fix this issue you can add this simple line to the $.format function to make sure the value is not null before merging it with your string.

This:
$.each(params, function(i, n) {
source = source.replace(new RegExp(“\\{” + i + “\\}”, “g”), n);
});

Becomes this:
$.each(params, function(i, n) {
if (n == null) n = ”;
source = source.replace(new RegExp(“\\{” + i + “\\}”, “g”), n);
});

See the video bellow for the bug and fix in action.

http://www.screencast.com/t/MGZlNjAxMDkt

Hope this helps!
Mitch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.