Skip to content
Browse files

Fixed #71 - improve existing time method and add time12h method for 1…

…2h am/pm time format
  • Loading branch information...
1 parent 1c0a140 commit 3f187a5d01f297c8db708a526953731ca0186a5f @jzaefferer jzaefferer committed
Showing with 20 additions and 3 deletions.
  1. +5 −3 additional-methods.js
  2. +1 −0 changelog.txt
  3. +14 −0 test/methods.js
View
8 additional-methods.js
@@ -157,9 +157,11 @@ jQuery.validator.addMethod("dateNL", 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);
- }, "Please enter a valid time, between 00:00 and 23:59"
-);
+ 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");
+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
View
1 changelog.txt
@@ -11,6 +11,7 @@
* 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 #184 - resetForm: should unset lastElement
+* Fixed #71 - improve existing time method and add time12h method for 12h am/pm time format
1.8.1
---
View
14 test/methods.js
@@ -492,12 +492,26 @@ test("time", function() {
var method = methodTest("time");
ok( method("00:00"), "Valid time, lower bound" );
ok( method("23:59"), "Valid time, upper bound" );
+ ok( !method("00:60"), "Invalid time" );
ok( !method("24:60"), "Invalid time" );
ok( !method("24:00"), "Invalid time" );
ok( !method("29:59"), "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() {
var method = methodTest("minWords");
ok( method("hello worlds", 2), "plain text, valid" );

0 comments on commit 3f187a5

Please sign in to comment.
Something went wrong with that request. Please try again.