Skip to content

Commit 3f187a5

Browse files
committed
Fixed jquery-validation#71 - improve existing time method and add time12h method for 12h am/pm time format
1 parent 1c0a140 commit 3f187a5

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

additional-methods.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ jQuery.validator.addMethod("dateNL", function(value, element) {
157157
);
158158

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

164166
/**
165167
* matches US phone number format

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Improved email method to not allow the dot at the end (valid by RFC, but unwanted here). Fixes #143
1212
* Fixed swedish and norwedian translations, min/max messages got switched. Fixes #181
1313
* Fixed #184 - resetForm: should unset lastElement
14+
* Fixed #71 - improve existing time method and add time12h method for 12h am/pm time format
1415

1516
1.8.1
1617
---

test/methods.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,26 @@ test("time", function() {
492492
var method = methodTest("time");
493493
ok( method("00:00"), "Valid time, lower bound" );
494494
ok( method("23:59"), "Valid time, upper bound" );
495+
ok( !method("00:60"), "Invalid time" );
495496
ok( !method("24:60"), "Invalid time" );
496497
ok( !method("24:00"), "Invalid time" );
497498
ok( !method("29:59"), "Invalid time" );
498499
ok( !method("30:00"), "Invalid time" );
499500
});
500501

502+
test("time12h", function() {
503+
var method = methodTest("time12h");
504+
ok( method("12:00 AM"), "Valid time, lower bound, am" );
505+
ok( method("11:59 AM"), "Valid time, upper bound, am" );
506+
ok( method("12:00 PM"), "Valid time, lower bound, pm" );
507+
ok( method("11:59 PM"), "Valid time, upper bound, pm" );
508+
ok( method("11:59 am"), "Valid time, also accept lowercase" );
509+
ok( method("11:59 pm"), "Valid time, also accept lowercase" );
510+
ok( !method("12:00"), "Invalid time" );
511+
ok( !method("12:61 am"), "Invalid time" );
512+
ok( !method("13:00 am"), "Invalid time" );
513+
});
514+
501515
test("minWords", function() {
502516
var method = methodTest("minWords");
503517
ok( method("hello worlds", 2), "plain text, valid" );

0 commit comments

Comments
 (0)