Skip to content

Commit 5dda6da

Browse files
gavachojzaefferer
authored andcommitted
Pattern method: Convert string param to RegExp. Fixes issue jquery-validation#223
1 parent 520b6b7 commit 5dda6da

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

additional-methods.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ jQuery.validator.addMethod("ipv6", function(value, element, param) {
295295
* @cat Plugins/Validate/Methods
296296
*/
297297
jQuery.validator.addMethod("pattern", function(value, element, param) {
298-
return this.optional(element) || param.test(value);
298+
if (this.optional(element)) {
299+
return true;
300+
}
301+
if (typeof param === 'string') {
302+
param = new RegExp('^(?:' + param + ')$');
303+
}
304+
return param.test(value);
299305
}, "Invalid format.");
300306

test/methods.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ test("maxWords", function() {
533533

534534
test("pattern", function() {
535535
var method = methodTest("pattern");
536+
ok( method( "AR1004", "AR\\d{4}" ), "Correct format for the given RegExp" );
536537
ok( method( "AR1004", /^AR\d{4}$/ ), "Correct format for the given RegExp" );
537538
ok( !method( "BR1004", /^AR\d{4}$/ ), "Invalid format for the given RegExp" );
538539
});

0 commit comments

Comments
 (0)