|
235 | 235 | }
|
236 | 236 |
|
237 | 237 | 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 | + |
239 | 240 | return Math.ceil((((date - first) / 86400000) + first.getDay() + 1) / 7);
|
240 | 241 | }
|
241 | 242 |
|
|
259 | 260 |
|
260 | 261 | function createDate(dateArray) {
|
261 | 262 | var i = 0,
|
262 |
| - date = []; |
| 263 | + date = [], |
| 264 | + isNum = false; |
263 | 265 |
|
264 | 266 | 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]; |
266 | 274 | }
|
267 | 275 |
|
268 | 276 | return new Date(date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
|
|
281 | 289 | for (i; i < length; i += 1) {
|
282 | 290 | input = (getParts(parts[i]).exec(string) || [])[0];
|
283 | 291 |
|
284 |
| - if (input) { |
285 |
| - string = string.slice(string.indexOf(input) + input.length); |
| 292 | + if (!input) { |
| 293 | + continue; |
286 | 294 | }
|
287 | 295 |
|
| 296 | + string = string.slice(string.indexOf(input) + input.length); |
288 | 297 | createDateArray(parts[i], input, dateArray);
|
289 | 298 | }
|
290 | 299 |
|
|
298 | 307 | }
|
299 | 308 |
|
300 | 309 | function formatDate(dateObj, format) {
|
| 310 | + if (dateObj === false) { |
| 311 | + return false; |
| 312 | + } |
| 313 | + |
| 314 | + if (format === undefined) { |
| 315 | + format = 'yyyy-mm-dd'; |
| 316 | + } |
| 317 | + |
301 | 318 | var parts = format.match(tokens) || [],
|
302 | 319 | length = parts.length,
|
303 | 320 | i = 0;
|
|
329 | 346 | color: /^#[0-9A-F]{6}$/i,
|
330 | 347 | email: /^[a-zA-Z0-9.!#$%&’*+\/=?\^_`{|}~\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*$/,
|
331 | 348 | number: /^-?\d*\.?\d*$/,
|
332 |
| - time: /^([01][0-9]|2[0-3])(:([0-5][0-9])){2}$/, |
333 | 349 | url: /^\s*https?:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?\s*$/
|
334 | 350 | },
|
335 | 351 |
|
|
338 | 354 | spaces: /,\s*/
|
339 | 355 | },
|
340 | 356 |
|
341 |
| - minMax = function (value, min, max, step, type) { |
| 357 | + minMax = function (value, min, max, step) { |
342 | 358 | var result = true,
|
343 | 359 | msg = $.validatr.messages.range.base;
|
344 | 360 |
|
|
368 | 384 | };
|
369 | 385 | },
|
370 | 386 |
|
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] ); |
373 | 390 | },
|
374 | 391 |
|
375 | 392 | formatMessage = function (message, type, min, max) {
|
|
403 | 420 |
|
404 | 421 | date: function (element) {
|
405 | 422 | var $element = $(element),
|
406 |
| - format = getFormat(element), |
| 423 | + format = getFormat(element, 'date'), |
407 | 424 | value = Format.date(element.value, format) || false,
|
408 | 425 | min = $element.attr('min') ? Format.isoDate($element.attr('min')) : false,
|
409 | 426 | max = $element.attr('max') ? Format.isoDate($element.attr('max')) : false,
|
410 | 427 | step = false,
|
411 |
| - result = minMax(value, min, max, step, 'date'); |
| 428 | + result = minMax(value, min, max, step); |
412 | 429 |
|
413 | 430 | if (!result.valid) {
|
414 | 431 | result.message = formatMessage(result.message, 'date', Format.toString(min, format), Format.toString(max, format));
|
|
441 | 458 | };
|
442 | 459 | },
|
443 | 460 |
|
| 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 | + |
444 | 476 | number: function (element) {
|
445 | 477 | var value = element.value.replace(',', ''),
|
446 | 478 | num = rules.number.test(value) ? parseFloat(value) : false,
|
|
490 | 522 | };
|
491 | 523 | },
|
492 | 524 |
|
493 |
| - time: function (element) { |
494 |
| - return { |
495 |
| - valid: rules.time.test(element.value), |
496 |
| - message: $.validatr.messages.time |
497 |
| - }; |
498 |
| - }, |
499 |
| - |
500 | 525 | url: function (element) {
|
501 | 526 | return {
|
502 | 527 | valid: rules.url.test(element.value),
|
|
1003 | 1028 | $.expr[':'].validatr = function(elem) {
|
1004 | 1029 | return !!$.data(elem, 'validatr');
|
1005 | 1030 | };
|
1006 |
| - |
1007 |
| - console.log(Format.toString(new Date(2013, 0, 14), 'yyyy-Www')); |
1008 | 1031 | }(this, this.document, jQuery));
|
0 commit comments