Skip to content

Commit

Permalink
Fixed jquery-validation#71 - improve existing time method and add tim…
Browse files Browse the repository at this point in the history
…e12h method for 12h am/pm time format
  • Loading branch information
jzaefferer committed Sep 13, 2011
1 parent 1c0a140 commit 3f187a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions additional-methods.js
Expand Up @@ -157,9 +157,11 @@ jQuery.validator.addMethod("dateNL", function(value, element) {
); );


jQuery.validator.addMethod("time", function(value, element) { jQuery.validator.addMethod("time", function(value, element) {
return this.optional(element) || /^([01][0-9])|(2[0123]):([0-5])([0-9])$/.test(value); return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){0,2}$/.test(value);
}, "Please enter a valid time, between 00:00 and 23:59" }, "Please enter a valid time, between 00:00 and 23:59");
); jQuery.validator.addMethod("time12h", function(value, element) {
return this.optional(element) || /^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))$/i.test(value);
}, "Please enter a valid time, between 00:00 am and 12:00 pm");


/** /**
* matches US phone number format * matches US phone number format
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Expand Up @@ -11,6 +11,7 @@
* Improved email method to not allow the dot at the end (valid by RFC, but unwanted here). Fixes #143 * Improved email method to not allow the dot at the end (valid by RFC, but unwanted here). Fixes #143
* Fixed swedish and norwedian translations, min/max messages got switched. Fixes #181 * Fixed swedish and norwedian translations, min/max messages got switched. Fixes #181
* Fixed #184 - resetForm: should unset lastElement * Fixed #184 - resetForm: should unset lastElement
* Fixed #71 - improve existing time method and add time12h method for 12h am/pm time format


1.8.1 1.8.1
--- ---
Expand Down
14 changes: 14 additions & 0 deletions test/methods.js
Expand Up @@ -492,12 +492,26 @@ test("time", function() {
var method = methodTest("time"); var method = methodTest("time");
ok( method("00:00"), "Valid time, lower bound" ); ok( method("00:00"), "Valid time, lower bound" );
ok( method("23:59"), "Valid time, upper bound" ); ok( method("23:59"), "Valid time, upper bound" );
ok( !method("00:60"), "Invalid time" );
ok( !method("24:60"), "Invalid time" ); ok( !method("24:60"), "Invalid time" );
ok( !method("24:00"), "Invalid time" ); ok( !method("24:00"), "Invalid time" );
ok( !method("29:59"), "Invalid time" ); ok( !method("29:59"), "Invalid time" );
ok( !method("30:00"), "Invalid time" ); ok( !method("30:00"), "Invalid time" );
}); });


test("time12h", function() {
var method = methodTest("time12h");
ok( method("12:00 AM"), "Valid time, lower bound, am" );
ok( method("11:59 AM"), "Valid time, upper bound, am" );
ok( method("12:00 PM"), "Valid time, lower bound, pm" );
ok( method("11:59 PM"), "Valid time, upper bound, pm" );
ok( method("11:59 am"), "Valid time, also accept lowercase" );
ok( method("11:59 pm"), "Valid time, also accept lowercase" );
ok( !method("12:00"), "Invalid time" );
ok( !method("12:61 am"), "Invalid time" );
ok( !method("13:00 am"), "Invalid time" );
});

test("minWords", function() { test("minWords", function() {
var method = methodTest("minWords"); var method = methodTest("minWords");
ok( method("hello worlds", 2), "plain text, valid" ); ok( method("hello worlds", 2), "plain text, valid" );
Expand Down

0 comments on commit 3f187a5

Please sign in to comment.