Skip to content

Commit f177fc0

Browse files
committed
Adding month support
Removing stub for time
1 parent f19e183 commit f177fc0

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

Diff for: src/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<input type="submit" value="Submit">
3737
</form>
3838
<form action="./">
39-
<div class="field"><label for="date">Date</label><input type="date" id="date" value="2013-03-19" min="2013-03-19" max="2013-03-21" step="2" name="date" required></div>
39+
<div class="field"><label for="date">Date</label><input type="date" id="date" min="2013-03-19" max="2013-03-21" step="2" name="date" required></div>
4040
<input type="reset">
4141
<input type="submit" value="Submit">
4242
</form>
@@ -51,7 +51,7 @@
5151
<input type="submit" value="Submit">
5252
</form>
5353
<form action="./">
54-
<div class="field"><label for="month">Month</label><input type="month" id="month" name="month" required></div>
54+
<div class="field"><label for="month">Month</label><input type="month" id="month" min="2013-03" max="2013-03" name="month" required></div>
5555
<input type="reset">
5656
<input type="submit" value="Submit">
5757
</form>

Diff for: src/js/validatr.js

+43-20
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@
235235
}
236236

237237
function getWeek(date) {
238-
var first = new Date(date.getFullYear(), 0, 1, 0, 0, 0);
238+
var first = new Date(date.getFullYear(), 0, 1, 0, 0, 0);
239+
239240
return Math.ceil((((date - first) / 86400000) + first.getDay() + 1) / 7);
240241
}
241242

@@ -259,10 +260,17 @@
259260

260261
function createDate(dateArray) {
261262
var i = 0,
262-
date = [];
263+
date = [],
264+
isNum = false;
263265

264266
for (i; i < 7; i += 1) {
265-
date[i] = (dateArray[i] == null) ? (i === 2 ? 1 : 0) : dateArray[i];
267+
isNum = typeof dateArray[i] === 'number';
268+
269+
if (!isNum && i < 2) {
270+
return false;
271+
}
272+
273+
date[i] = !isNum ? (i === 2 ? 1 : 0) : dateArray[i];
266274
}
267275

268276
return new Date(date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
@@ -281,10 +289,11 @@
281289
for (i; i < length; i += 1) {
282290
input = (getParts(parts[i]).exec(string) || [])[0];
283291

284-
if (input) {
285-
string = string.slice(string.indexOf(input) + input.length);
292+
if (!input) {
293+
continue;
286294
}
287295

296+
string = string.slice(string.indexOf(input) + input.length);
288297
createDateArray(parts[i], input, dateArray);
289298
}
290299

@@ -298,6 +307,14 @@
298307
}
299308

300309
function formatDate(dateObj, format) {
310+
if (dateObj === false) {
311+
return false;
312+
}
313+
314+
if (format === undefined) {
315+
format = 'yyyy-mm-dd';
316+
}
317+
301318
var parts = format.match(tokens) || [],
302319
length = parts.length,
303320
i = 0;
@@ -329,7 +346,6 @@
329346
color: /^#[0-9A-F]{6}$/i,
330347
email: /^[a-zA-Z0-9.!#$%&*+\/=?\^_`{|}~\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*$/,
331348
number: /^-?\d*\.?\d*$/,
332-
time: /^([01][0-9]|2[0-3])(:([0-5][0-9])){2}$/,
333349
url: /^\s*https?:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?\s*$/
334350
},
335351

@@ -338,7 +354,7 @@
338354
spaces: /,\s*/
339355
},
340356

341-
minMax = function (value, min, max, step, type) {
357+
minMax = function (value, min, max, step) {
342358
var result = true,
343359
msg = $.validatr.messages.range.base;
344360

@@ -368,8 +384,9 @@
368384
};
369385
},
370386

371-
getFormat = function (element) {
372-
return element.getAttribute('data-format') || ( $.data(element.form, 'validatr').options.dateFormat || $.fn.validatr.defaultOptions.dateFormat );
387+
getFormat = function (element, type) {
388+
type += 'Format';
389+
return element.getAttribute('data-format') || ( $.data(element.form, 'validatr').options[type] || $.fn.validatr.defaultOptions[type] );
373390
},
374391

375392
formatMessage = function (message, type, min, max) {
@@ -403,12 +420,12 @@
403420

404421
date: function (element) {
405422
var $element = $(element),
406-
format = getFormat(element),
423+
format = getFormat(element, 'date'),
407424
value = Format.date(element.value, format) || false,
408425
min = $element.attr('min') ? Format.isoDate($element.attr('min')) : false,
409426
max = $element.attr('max') ? Format.isoDate($element.attr('max')) : false,
410427
step = false,
411-
result = minMax(value, min, max, step, 'date');
428+
result = minMax(value, min, max, step);
412429

413430
if (!result.valid) {
414431
result.message = formatMessage(result.message, 'date', Format.toString(min, format), Format.toString(max, format));
@@ -441,6 +458,21 @@
441458
};
442459
},
443460

461+
month: function (element) {
462+
var $element = $(element),
463+
format = getFormat(element, 'month'),
464+
value = Format.date(element.value, format) || false,
465+
min = $element.attr('min') ? Format.isoDate($element.attr('min') + '01') : false,
466+
max = $element.attr('max') ? Format.isoDate($element.attr('max') + '01') : false,
467+
step = false,
468+
result = minMax(value, min, max, step);
469+
470+
if (!result.valid) {
471+
result.message = formatMessage(result.message, 'month', Format.toString(min, format), Format.toString(max, format));
472+
}
473+
return result;
474+
},
475+
444476
number: function (element) {
445477
var value = element.value.replace(',', ''),
446478
num = rules.number.test(value) ? parseFloat(value) : false,
@@ -490,13 +522,6 @@
490522
};
491523
},
492524

493-
time: function (element) {
494-
return {
495-
valid: rules.time.test(element.value),
496-
message: $.validatr.messages.time
497-
};
498-
},
499-
500525
url: function (element) {
501526
return {
502527
valid: rules.url.test(element.value),
@@ -1003,6 +1028,4 @@
10031028
$.expr[':'].validatr = function(elem) {
10041029
return !!$.data(elem, 'validatr');
10051030
};
1006-
1007-
console.log(Format.toString(new Date(2013, 0, 14), 'yyyy-Www'));
10081031
}(this, this.document, jQuery));

0 commit comments

Comments
 (0)