Permalink
Browse files

Additionals: Fix pattern method when parameter is a string

Reverts 37992c1

The pattern should match the entire value of the input, not just a subset.

Closes #1204
  • Loading branch information...
barryvdh authored and jzaefferer committed Jul 7, 2014
1 parent 9f4ba10 commit 4cecd736491bc81dc13f772704a70326f8a24f3c
Showing with 8 additions and 1 deletion.
  1. +1 −1 src/additional/pattern.js
  2. +7 −0 test/methods.js
@@ -16,7 +16,7 @@ $.validator.addMethod("pattern", function(value, element, param) {
return true;
}
if (typeof param === "string") {
- param = new RegExp(param);
+ param = new RegExp("^(?:" + param + ")$");
}
return param.test(value);
}, "Invalid format.");
View
@@ -918,6 +918,13 @@ test("pattern", function() {
ok( method( "AR1004", "AR\\d{4}" ), "Correct format for the given RegExp" );
ok( method( "AR1004", /^AR\d{4}$/ ), "Correct format for the given RegExp" );
ok(!method( "BR1004", /^AR\d{4}$/ ), "Invalid format for the given RegExp" );
+ ok( method( "1ABC", "[0-9][A-Z]{3}" ), "Correct format for the given RegExp" );
+ ok(!method( "ABC", "[0-9][A-Z]{3}" ), "Invalid format for the given RegExp" );
+ ok(!method( "1ABC DEF", "[0-9][A-Z]{3}" ), "Invalid format for the given RegExp" );
+ ok( method( "1ABCdef", "[a-zA-Z0-9]+" ), "Correct format for the given RegExp" );
+ ok(!method( "1ABC def", "[a-zA-Z0-9]+" ), "Invalid format for the given RegExp" );
+ ok( method( "2014-10-02", "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ), "Correct format for the given RegExp" );
+ ok(!method( "02-10-2014", "[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" ), "Invalid format for the given RegExp" );
});
function testCardTypeByNumber(number, cardname, expected) {

0 comments on commit 4cecd73

Please sign in to comment.