Skip to content

Commit 6e4d689

Browse files
committed
new feature, now possible to configure the dateformat expected when validating dates
1 parent bc5921c commit 6e4d689

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
99

1010
<script type="text/javascript" src="jquery.js"></script>
11-
<script type="text/javascript" src="jquery.formvalidator.min.js"></script>
11+
<script type="text/javascript" src="jquery.formvalidator.js"></script>
1212

1313
<link href="style.css" type="text/css" rel="stylesheet"/>
1414

jquery.formvalidator.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* (c) 2011 Victor Jonsson, Sweden.
1010
* Dual licensed under the MIT or GPL Version 2 licenses
1111
*
12-
* $version 1.1
12+
* $version 1.2 beta
1313
*
1414
*/
1515

@@ -83,7 +83,8 @@
8383
var config = {
8484
validationRuleAttribute : 'data-validation',
8585
errorElementClass : 'error', // Class that will be put on elements which value is invalid
86-
borderColorOnError : 'red'
86+
borderColorOnError : 'red',
87+
dateFormat : 'yyyy-mm-dd'
8788
};
8889

8990
if (settings)
@@ -105,7 +106,7 @@
105106
if(config.borderColorOnError != '')
106107
$(this).css('border-color', jQueryFormUtils.defaultBorderColor);
107108

108-
var validation = jQueryFormUtils.validateInput($(this), language, config.validationRuleAttribute);
109+
var validation = jQueryFormUtils.validateInput($(this), language, config);
109110

110111
if(validation === true)
111112
$(this).unbind('keyup');
@@ -145,7 +146,8 @@
145146
errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
146147
validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
147148
errorMessagePosition : 'top', // Can be either "top" or "element"
148-
scrollToTopOnError : true
149+
scrollToTopOnError : true,
150+
dateFormat : 'yyyy-mm-dd'
149151
};
150152

151153
/*
@@ -227,7 +229,7 @@
227229
var valid = jQueryFormUtils.validateInput(
228230
$(this),
229231
language,
230-
config.validationRuleAttribute,
232+
config,
231233
$(form)
232234
);
233235

@@ -365,50 +367,72 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
365367
* Is this a valid birth date YYYY-MM-DD
366368
* @return {Boolean}
367369
*/
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)
370373
return false;
371374

372375
var d = new Date();
373376
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];
375380

376381
if (year == currentYear) {
377-
var month = parseInt(val.substring(5, 7));
378382
var currentMonth = d.getMonth() + 1;
379383
if (month == currentMonth) {
380-
var day = parseInt(val.substring(8, 10));
381384
var currentDay = d.getDate();
382385
return day <= currentDay;
383386
}
384387
else
385388
return month < currentMonth;
386389
}
387390
else
388-
return year < currentYear && year > (currentYear - 122);
391+
return year < currentYear && year > (currentYear - 124); // we can not live for ever yet...
389392
};
390393

391394
/**
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
393397
* @param {String} val
394-
* @return {Boolean}
398+
* @return {Array}|{Boolean}
395399
*/
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+
397410
// enklast m�jliga...
398-
if (val.match(/^(\d{4})\-(\d{2})\-(\d{2})$/) == null)
411+
if (matches == null)
399412
return false;
400413

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);
403426
month = jQueryFormUtils.parseDateInt(month);
404427
day = jQueryFormUtils.parseDateInt(day);
405-
428+
year = jQueryFormUtils.parseDateInt(year);
429+
406430
if (month == 2 && day > 28 || month > 12 || month == 0)
407431
return false;
408432
if ((this.isShortMonth(month) && day > 30) || (!this.isShortMonth(month) && day > 31) || day == 0)
409433
return false;
410434

411-
return true;
435+
return [year, month, day];
412436
};
413437

414438
/**
@@ -580,9 +604,9 @@ jQueryFormUtils.validateDomain = function(val) {
580604
* @param {jQuery} form
581605
* @return {String}|{Boolean}
582606
*/
583-
jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form) {
607+
jQueryFormUtils.validateInput = function(el, language, config, form) {
584608
var value = jQuery.trim(el.val());
585-
var validationRules = el.attr(validationRuleAttr);
609+
var validationRules = el.attr(config.validationRuleAttribute);
586610

587611
if (typeof validationRules != 'undefined' && validationRules != null) {
588612

@@ -651,12 +675,12 @@ jQueryFormUtils.validateInput = function(el, language, validationRuleAttr, form)
651675
}
652676

653677
// 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)) {
655679
return language.badDate;
656680
}
657681

658682
// 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)) {
660684
return language.badDate;
661685
}
662686

0 commit comments

Comments
 (0)