|
9 | 9 | * (c) 2011 Victor Jonsson, Sweden.
|
10 | 10 | * Dual licensed under the MIT or GPL Version 2 licenses
|
11 | 11 | *
|
12 |
| -* $version 1.1 |
| 12 | +* $version 1.2 beta |
13 | 13 | *
|
14 | 14 | */
|
15 | 15 |
|
|
83 | 83 | var config = {
|
84 | 84 | validationRuleAttribute : 'data-validation',
|
85 | 85 | errorElementClass : 'error', // Class that will be put on elements which value is invalid
|
86 |
| - borderColorOnError : 'red' |
| 86 | + borderColorOnError : 'red', |
| 87 | + dateFormat : 'yyyy-mm-dd' |
87 | 88 | };
|
88 | 89 |
|
89 | 90 | if (settings)
|
|
105 | 106 | if(config.borderColorOnError != '')
|
106 | 107 | $(this).css('border-color', jQueryFormUtils.defaultBorderColor);
|
107 | 108 |
|
108 |
| - var validation = jQueryFormUtils.validateInput($(this), language, config.validationRuleAttribute); |
| 109 | + var validation = jQueryFormUtils.validateInput($(this), language, config); |
109 | 110 |
|
110 | 111 | if(validation === true)
|
111 | 112 | $(this).unbind('keyup');
|
|
145 | 146 | errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
|
146 | 147 | validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
|
147 | 148 | errorMessagePosition : 'top', // Can be either "top" or "element"
|
148 |
| - scrollToTopOnError : true |
| 149 | + scrollToTopOnError : true, |
| 150 | + dateFormat : 'yyyy-mm-dd' |
149 | 151 | };
|
150 | 152 |
|
151 | 153 | /*
|
|
227 | 229 | var valid = jQueryFormUtils.validateInput(
|
228 | 230 | $(this),
|
229 | 231 | language,
|
230 |
| - config.validationRuleAttribute, |
| 232 | + config, |
231 | 233 | $(form)
|
232 | 234 | );
|
233 | 235 |
|
@@ -365,50 +367,72 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
|
365 | 367 | * Is this a valid birth date YYYY-MM-DD
|
366 | 368 | * @return {Boolean}
|
367 | 369 | */
|
368 |
| -jQueryFormUtils.validateBirthdate = function(val) { |
369 |
| - if (!this.validateDate(val)) |
| 370 | +jQueryFormUtils.validateBirthdate = function(val, dateFormat) { |
| 371 | + var inputDate = this.validateDate(val, dateFormat); |
| 372 | + if (!inputDate) |
370 | 373 | return false;
|
371 | 374 |
|
372 | 375 | var d = new Date();
|
373 | 376 | var currentYear = d.getFullYear();
|
374 |
| - var year = parseInt(val.substring(0, 4)); |
| 377 | + var year = inputDate[0]; |
| 378 | + var month = inputDate[1]; |
| 379 | + var day = inputDate[2]; |
375 | 380 |
|
376 | 381 | if (year == currentYear) {
|
377 |
| - var month = parseInt(val.substring(5, 7)); |
378 | 382 | var currentMonth = d.getMonth() + 1;
|
379 | 383 | if (month == currentMonth) {
|
380 |
| - var day = parseInt(val.substring(8, 10)); |
381 | 384 | var currentDay = d.getDate();
|
382 | 385 | return day <= currentDay;
|
383 | 386 | }
|
384 | 387 | else
|
385 | 388 | return month < currentMonth;
|
386 | 389 | }
|
387 | 390 | else
|
388 |
| - return year < currentYear && year > (currentYear - 122); |
| 391 | + return year < currentYear && year > (currentYear - 124); // we can not live for ever yet... |
389 | 392 | };
|
390 | 393 |
|
391 | 394 | /**
|
392 |
| - * Is it a correct date YYYY-MM-DD |
| 395 | + * Is it a correct date YYYY-MM-DD. Will return false if not, otherwise |
| 396 | + * an array 0=>year 1=>month 2=>day |
393 | 397 | * @param {String} val
|
394 |
| - * @return {Boolean} |
| 398 | + * @return {Array}|{Boolean} |
395 | 399 | */
|
396 |
| -jQueryFormUtils.validateDate = function(val) { |
| 400 | +jQueryFormUtils.validateDate = function(val, dateFormat) { |
| 401 | + var divider = dateFormat.replace(/[a-zA-Z]/gi, '').substring(0,1); |
| 402 | + var regexp = '^'; |
| 403 | + var formatParts = dateFormat.split(divider); |
| 404 | + for(var i=0; i < formatParts.length; i++) |
| 405 | + regexp += (i > 0 ? '\\'+divider:'') + '(\\d{'+formatParts[i].length+'})'; |
| 406 | + regexp += '$'; |
| 407 | + |
| 408 | + var matches = val.match(new RegExp(regexp)); |
| 409 | + |
397 | 410 | // enklast m�jliga...
|
398 |
| - if (val.match(/^(\d{4})\-(\d{2})\-(\d{2})$/) == null) |
| 411 | + if (matches == null) |
399 | 412 | return false;
|
400 | 413 |
|
401 |
| - var month = val.substring(5, 8); |
402 |
| - var day = val.substring(8, 11); |
| 414 | + var findDateUnit = function(unit, formatParts, matches) { |
| 415 | + for(var i=0; i < formatParts.length; i++) { |
| 416 | + if(formatParts[i].substring(0,1) == unit) { |
| 417 | + return matches[i+1]; |
| 418 | + } |
| 419 | + } |
| 420 | + return -1; |
| 421 | + }; |
| 422 | + |
| 423 | + var month = findDateUnit('m', formatParts, matches); |
| 424 | + var day = findDateUnit('d', formatParts, matches); |
| 425 | + var year = findDateUnit('y', formatParts, matches); |
403 | 426 | month = jQueryFormUtils.parseDateInt(month);
|
404 | 427 | day = jQueryFormUtils.parseDateInt(day);
|
405 |
| - |
| 428 | + year = jQueryFormUtils.parseDateInt(year); |
| 429 | + |
406 | 430 | if (month == 2 && day > 28 || month > 12 || month == 0)
|
407 | 431 | return false;
|
408 | 432 | if ((this.isShortMonth(month) && day > 30) || (!this.isShortMonth(month) && day > 31) || day == 0)
|
409 | 433 | return false;
|
410 | 434 |
|
411 |
| - return true; |
| 435 | + return [year, month, day]; |
412 | 436 | };
|
413 | 437 |
|
414 | 438 | /**
|
@@ -580,9 +604,9 @@ jQueryFormUtils.validateDomain = function(val) {
|
580 | 604 | * @param {jQuery} form
|
581 | 605 | * @return {String}|{Boolean}
|
582 | 606 | */
|
583 |
| -jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form) { |
| 607 | +jQueryFormUtils.validateInput = function(el, language, config, form) { |
584 | 608 | var value = jQuery.trim(el.val());
|
585 |
| - var validationRules = el.attr(validationRuleAttr); |
| 609 | + var validationRules = el.attr(config.validationRuleAttribute); |
586 | 610 |
|
587 | 611 | if (typeof validationRules != 'undefined' && validationRules != null) {
|
588 | 612 |
|
@@ -651,12 +675,12 @@ jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form)
|
651 | 675 | }
|
652 | 676 |
|
653 | 677 | // Date
|
654 |
| - else if (validationRules.indexOf('validate_date') > -1 && !jQueryFormUtils.validateDate(value)) { |
| 678 | + else if (validationRules.indexOf('validate_date') > -1 && !jQueryFormUtils.validateDate(value, config.dateFormat)) { |
655 | 679 | return language.badDate;
|
656 | 680 | }
|
657 | 681 |
|
658 | 682 | // Birth date
|
659 |
| - else if (validationRules.indexOf('validate_birthdate') > -1 && !jQueryFormUtils.validateBirthdate(value)) { |
| 683 | + else if (validationRules.indexOf('validate_birthdate') > -1 && !jQueryFormUtils.validateBirthdate(value, config.dateFormat)) { |
660 | 684 | return language.badDate;
|
661 | 685 | }
|
662 | 686 |
|
|
0 commit comments