feba8af35288d755900ee3f71edc602e20042ac6
2 * This file is part of the TYPO3 CMS project.
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
11 * The TYPO3 project - inspiring people to share!
15 * Module: TYPO3/CMS/Backend/FormEngineValidation
16 * Contains all JS functions related to TYPO3 TCEforms/FormEngineValidation
19 define(['jquery', 'TYPO3/CMS/Backend/FormEngine', 'moment'], function ($, FormEngine
, moment
) {
22 * The main FormEngineValidation object
24 * @type {{rulesSelector: string, inputSelector: string, markerSelector: string, dateTimeSelector: string, groupFieldHiddenElement: string, relatedFieldSelector: string, errorClass: string, lastYear: number, lastDate: number, lastTime: number, refDate: Date, USmode: number, passwordDummy: string}}
25 * @exports TYPO3/CMS/Backend/FormEngineValidation
27 var FormEngineValidation
= {
28 rulesSelector
: '[data-formengine-validation-rules]',
29 inputSelector
: '[data-formengine-input-params]',
30 markerSelector
: '.t3js-formengine-validation-marker',
31 dateTimeSelector
: '.t3js-datetimepicker',
32 groupFieldHiddenElement
: '.t3js-formengine-field-group input[type=hidden]',
33 relatedFieldSelector
: '[data-relatedfieldname]',
34 errorClass
: 'has-error',
40 passwordDummy
: '********'
44 * Initialize validation for the first time
46 FormEngineValidation
.initialize = function() {
47 $(document
).find('.' + FormEngineValidation
.errorClass
).removeClass(FormEngineValidation
.errorClass
);
49 // Initialize input fields
50 FormEngineValidation
.initializeInputFields().promise().done(function () {
51 // Bind to field changes
52 $(document
).on('change', FormEngineValidation
.rulesSelector
, function() {
53 FormEngineValidation
.validate();
54 var $paletteField
= $(this).closest('.t3js-formengine-palette-field');
55 $paletteField
.addClass('has-change');
58 $(document
).on('dp.change', FormEngineValidation
.dateTimeSelector
, function(event
) {
59 FormEngineValidation
.validate();
60 var $paletteField
= $(this).closest('.t3js-formengine-palette-field');
61 $paletteField
.addClass('has-change');
65 var today
= new Date();
66 FormEngineValidation
.lastYear
= FormEngineValidation
.getYear(today
);
67 FormEngineValidation
.lastDate
= FormEngineValidation
.getDate(today
);
68 FormEngineValidation
.lastTime
= 0;
69 FormEngineValidation
.refDate
= today
;
70 FormEngineValidation
.USmode
= 0;
71 FormEngineValidation
.validate();
75 * Initialize all input fields
79 FormEngineValidation
.initializeInputFields = function() {
80 return $(document
).find(FormEngineValidation
.inputSelector
).each(function() {
81 var config
= $(this).data('formengine-input-params');
82 var fieldName
= config
.field
;
83 var $field
= $('[name="' + fieldName
+ '"]');
85 // ignore fields which already have been initialized
86 if ($field
.data('main-field') === undefined) {
87 $field
.data('main-field', fieldName
);
88 $field
.data('config', config
);
89 FormEngineValidation
.initializeInputField(fieldName
);
96 * @param {Number} mode
98 FormEngineValidation
.setUsMode = function(mode
) {
99 FormEngineValidation
.USmode
= mode
;
103 * Initialize field by name
105 * @param {String} fieldName
107 FormEngineValidation
.initializeInputField = function(fieldName
) {
108 var $field
= $('[name="' + fieldName
+ '"]');
109 var $humanReadableField
= $('[data-formengine-input-name="' + fieldName
+ '"]');
110 var $mainField
= $('[name="' + $field
.data('main-field') + '"]');
111 if ($mainField
.length
=== 0) {
115 var config
= $mainField
.data('config');
116 if (typeof config
!== 'undefined') {
117 var evalList
= FormEngineValidation
.trimExplode(',', config
.evalList
);
118 var value
= $field
.val();
120 for (var i
= 0; i
< evalList
.length
; i
++) {
121 value
= FormEngineValidation
.formatValue(evalList
[i
], value
, config
)
123 // Prevent password fields to be overwritten with original value
124 if (value
.length
&& $humanReadableField
.attr('type') != 'password') {
125 $humanReadableField
.val(value
);
128 $humanReadableField
.data('main-field', fieldName
);
129 $humanReadableField
.data('config', config
);
130 $humanReadableField
.on('change', function() {
131 FormEngineValidation
.updateInputField($(this).attr('data-formengine-input-name'));
133 $humanReadableField
.on('keyup', FormEngineValidation
.validate
);
135 // add the attribute so that acceptance tests can know when the field initialization has completed
136 $humanReadableField
.attr('data-formengine-input-initialized', 'true');
142 * @param {String} type
143 * @param {String} value
144 * @param {Object} config
147 FormEngineValidation
.formatValue = function(type
, value
, config
) {
151 // poor man’s ISO-8601 detection: if we have a "-" in it, it apparently is not an integer.
152 if (value
.toString().indexOf('-') > 0) {
153 var date
= moment(value
).utc();
154 if (FormEngineValidation
.USmode
) {
155 theString
= date
.format('MM-DD-YYYY');
157 theString
= date
.format('DD-MM-YYYY');
160 var parsedInt
= parseInt(value
);
164 theTime
= new Date(parsedInt
* 1000);
165 if (FormEngineValidation
.USmode
) {
166 theString
= (theTime
.getUTCMonth() + 1) + '-' + theTime
.getUTCDate() + '-' + this.getYear(theTime
);
168 theString
= theTime
.getUTCDate() + '-' + (theTime
.getUTCMonth() + 1) + '-' + this.getYear(theTime
);
173 if (value
.toString().indexOf('-') <= 0 && !parseInt(value
)) {
176 theString
= FormEngineValidation
.formatValue('time', value
, config
) + ' ' + FormEngineValidation
.formatValue('date', value
, config
);
180 if (value
.toString().indexOf('-') > 0) {
181 var date
= moment(value
).utc();
182 if (type
== 'timesec') {
183 theString
= date
.format('HH:mm:ss');
185 theString
= date
.format('HH:mm');
188 var parsedInt
= parseInt(value
);
189 if (!parsedInt
&& value
.toString() !== '0') {
192 var theTime
= new Date(parsedInt
* 1000);
193 var h
= theTime
.getUTCHours();
194 var m
= theTime
.getUTCMinutes();
195 var s
= theTime
.getUTCSeconds();
196 theString
= h
+ ':' + ((m
< 10) ? '0' : '') + m
+ ((type
== 'timesec') ? ':' + ((s
< 10) ? '0' : '') + s
: '');
200 theString
= (value
) ? FormEngineValidation
.passwordDummy
: '';
209 * Update input field after change
211 * @param {String} fieldName
213 FormEngineValidation
.updateInputField = function(fieldName
) {
214 var $field
= $('[name="' + fieldName
+ '"]');
215 var $mainField
= $('[name="' + $field
.data('main-field') + '"]');
216 if ($mainField
.length
=== 0) {
219 var $humanReadableField
= $('[data-formengine-input-name="' + $mainField
.attr('name') + '"]');
221 var config
= $mainField
.data('config');
222 if (typeof config
!== 'undefined') {
223 var evalList
= FormEngineValidation
.trimExplode(',', config
.evalList
);
224 var newValue
= $humanReadableField
.val();
227 for (i
= 0; i
< evalList
.length
; i
++) {
228 newValue
= FormEngineValidation
.processValue(evalList
[i
], newValue
, config
);
231 var formattedValue
= newValue
;
232 for (i
= 0; i
< evalList
.length
; i
++) {
233 formattedValue
= FormEngineValidation
.formatValue(evalList
[i
], formattedValue
, config
);
236 $mainField
.val(newValue
);
237 $humanReadableField
.val(formattedValue
);
242 * Run validation for field
244 * @param {Object} $field
245 * @param {String} [value=$field.val()]
248 FormEngineValidation
.validateField = function($field
, value
) {
249 value
= value
|| $field
.val() || '';
251 var rules
= $field
.data('formengine-validation-rules');
252 var markParent
= false;
254 // keep the original value, validateField should not alter it
255 var returnValue
= value
;
260 if (!$.isArray(value
)) {
261 value
= FormEngineValidation
.ltrim(value
);
264 $.each(rules
, function(k
, rule
) {
269 $field
.closest(FormEngineValidation
.markerSelector
).addClass(FormEngineValidation
.errorClass
);
274 if (rule
.minItems
|| rule
.maxItems
) {
275 $relatedField
= $(document
).find('[name="' + $field
.data('relatedfieldname') + '"]');
276 if ($relatedField
.length
) {
277 selected
= FormEngineValidation
.trimExplode(',', $relatedField
.val()).length
;
279 selected
= $field
.val();
281 if (typeof rule
.minItems
!== 'undefined') {
282 minItems
= rule
.minItems
* 1;
283 if (!isNaN(minItems
) && selected
< minItems
) {
287 if (typeof rule
.maxItems
!== 'undefined') {
288 maxItems
= rule
.maxItems
* 1;
289 if (!isNaN(maxItems
) && selected
> maxItems
) {
294 if (typeof rule
.lower
!== 'undefined') {
295 var minValue
= rule
.lower
* 1;
296 if (!isNaN(minValue
) && value
< minValue
) {
300 if (typeof rule
.upper
!== 'undefined') {
301 var maxValue
= rule
.upper
* 1;
302 if (!isNaN(maxValue
) && value
> maxValue
) {
309 if (rule
.minItems
|| rule
.maxItems
) {
310 $relatedField
= $(document
).find('[name="' + $field
.data('relatedfieldname') + '"]');
311 if ($relatedField
.length
) {
312 selected
= FormEngineValidation
.trimExplode(',', $relatedField
.val()).length
;
314 selected
= $field
.find('option:selected').length
;
316 if (typeof rule
.minItems
!== 'undefined') {
317 minItems
= rule
.minItems
* 1;
318 if (!isNaN(minItems
) && selected
< minItems
) {
322 if (typeof rule
.maxItems
!== 'undefined') {
323 maxItems
= rule
.maxItems
* 1;
324 if (!isNaN(maxItems
) && selected
> maxItems
) {
331 if (rule
.minItems
|| rule
.maxItems
) {
332 selected
= $field
.find('option').length
;
333 if (typeof rule
.minItems
!== 'undefined') {
334 minItems
= rule
.minItems
* 1;
335 if (!isNaN(minItems
) && selected
< minItems
) {
339 if (typeof rule
.maxItems
!== 'undefined') {
340 maxItems
= rule
.maxItems
* 1;
341 if (!isNaN(maxItems
) && selected
> maxItems
) {
348 if (rule
.minItems
|| rule
.maxItems
) {
349 selected
= FormEngineValidation
.trimExplode(',', $field
.val()).length
;
350 if (typeof rule
.minItems
!== 'undefined') {
351 minItems
= rule
.minItems
* 1;
352 if (!isNaN(minItems
) && selected
< minItems
) {
356 if (typeof rule
.maxItems
!== 'undefined') {
357 maxItems
= rule
.maxItems
* 1;
358 if (!isNaN(maxItems
) && selected
> maxItems
) {
365 // unknown type null, we ignore it
371 $field
.closest(FormEngineValidation
.markerSelector
).addClass(FormEngineValidation
.errorClass
);
374 FormEngineValidation
.markParentTab($field
);
380 * Process a value by given command and config
382 * @param {String} command
383 * @param {String} value
384 * @param {Array} config
387 FormEngineValidation
.processValue = function(command
, value
, config
) {
392 var returnValue
= value
;
399 for (a
= 0; a
< value
.length
; a
++) {
400 theChar
= value
.substr(a
, 1);
401 var special
= (theChar
=== '_' || theChar
=== '-');
402 var alpha
= (theChar
>= 'a' && theChar
<= 'z') || (theChar
>= 'A' && theChar
<= 'Z');
403 var num
= (theChar
>= '0' && theChar
<= '9');
417 if (alpha
|| num
|| special
) {
418 newString
+= theChar
;
421 if (newString
!== value
) {
422 returnValue
= newString
;
427 theValue
= '' + value
;
428 for (a
= 0; a
< theValue
.length
; a
++) {
429 var theChar
= theValue
.substr(a
, 1);
430 if (config
.is_in
.indexOf(theChar
) != -1) {
431 newString
+= theChar
;
435 newString
= theValue
;
437 returnValue
= newString
;
440 returnValue
= '' + value
.replace(/ /g
, '');
444 returnValue
= MD5(value
);
448 returnValue
= value
.toUpperCase();
451 returnValue
= value
.toLowerCase();
455 returnValue
= FormEngineValidation
.parseInt(value
);
460 returnValue
= FormEngineValidation
.parseDouble(value
);
464 returnValue
= String(value
).trim();
468 theCmd
= value
.substr(0, 1);
469 returnValue
= FormEngineValidation
.parseDateTime(value
, theCmd
);
474 theCmd
= value
.substr(0, 1);
475 returnValue
= FormEngineValidation
.parseDate(value
, theCmd
);
481 theCmd
= value
.substr(0, 1);
482 returnValue
= FormEngineValidation
.parseTime(value
, theCmd
, command
);
487 theCmd
= value
.substr(0, 1);
488 returnValue
= FormEngineValidation
.parseYear(value
, theCmd
);
492 // unknown type null, we ignore it
495 // password is only a display evaluation, we ignore it
498 if (typeof TBE_EDITOR
.customEvalFunctions
!== 'undefined' && typeof TBE_EDITOR
.customEvalFunctions
[command
] === 'function') {
499 returnValue
= TBE_EDITOR
.customEvalFunctions
[command
](value
);
506 * Validate the complete form
508 FormEngineValidation
.validate = function() {
509 $(document
).find(FormEngineValidation
.markerSelector
+ ', .t3js-tabmenu-item')
510 .removeClass(FormEngineValidation
.errorClass
)
511 .removeClass('has-validation-error');
513 $(FormEngineValidation
.rulesSelector
).each(function() {
514 var $field
= $(this);
515 if (!$field
.closest('.t3js-flex-section-deleted, .t3js-inline-record-deleted').length
) {
516 var modified
= false;
517 var currentValue
= $field
.val();
518 var newValue
= FormEngineValidation
.validateField($field
, currentValue
);
519 if ($.isArray(newValue
) && $.isArray(currentValue
)) {
520 // handling for multi-selects
521 if (newValue
.length
!== currentValue
.length
) {
524 for (var i
= 0; i
< newValue
.length
; i
++) {
525 if (newValue
[i
] !== currentValue
[i
]) {
531 } else if (newValue
.length
&& currentValue
!== newValue
) {
535 $field
.val(newValue
);
539 $(document
).trigger('t3-formengine-postfieldvalidation');
543 * Helper function to get clean trimmed array from comma list
545 * @param {String} delimiter
546 * @param {String} string
549 FormEngineValidation
.trimExplode = function(delimiter
, string
) {
551 var items
= string
.split(delimiter
);
552 for (var i
=0; i
<items
.length
; i
++) {
553 var item
= items
[i
].trim();
554 if (item
.length
> 0) {
562 * Parse value to integer
564 * @param {(Number|String)} value
567 FormEngineValidation
.parseInt = function(value
) {
568 var theVal
= '' + value
,
575 returnValue
= parseInt(theVal
, 10);
576 if (isNaN(returnValue
)) {
583 * Parse value to double
585 * @param {String} value
588 FormEngineValidation
.parseDouble = function(value
) {
589 var theVal
= '' + value
;
590 theVal
= theVal
.replace(/[^0-9,\.-]/g, '');
591 var negative
= theVal
.substring(0, 1) === '-';
592 theVal
= theVal
.replace(/-/g
, '');
593 theVal
= theVal
.replace(/,/g
, '.');
594 if (theVal
.indexOf('.') === -1) {
597 var parts
= theVal
.split('.');
598 var dec
= parts
.pop();
599 theVal
= Number(parts
.join('') + '.' + dec
);
603 theVal
= theVal
.toFixed(2);
610 * @param {String} value
613 FormEngineValidation
.ltrim = function(value
) {
614 var theVal
= '' + value
;
618 return theVal
.replace(/^\s+/, '');
623 * @param {String} value
626 FormEngineValidation
.btrim = function(value
) {
627 var theVal
= '' + value
;
631 return theVal
.replace(/\s+$/, '');
635 * Parse datetime value
637 * @param {String} value
638 * @param {String} command
641 FormEngineValidation
.parseDateTime = function(value
, command
) {
642 var today
= new Date();
643 var values
= FormEngineValidation
.split(value
);
649 FormEngineValidation
.lastTime
= FormEngineValidation
.convertClientTimestampToUTC(FormEngineValidation
.getTimestamp(today
), 0);
650 if (values
.valPol
[1]) {
651 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
656 if (FormEngineValidation
.lastTime
== 0) {
657 FormEngineValidation
.lastTime
= FormEngineValidation
.convertClientTimestampToUTC(FormEngineValidation
.getTimestamp(today
), 0);
659 if (values
.valPol
[1]) {
660 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
664 var index
= value
.indexOf(' ');
666 var dateVal
= FormEngineValidation
.parseDate(value
.substr(index
, value
.length
), value
.substr(0, 1));
667 // set refDate so that evalFunc_input on time will work with correct DST information
668 FormEngineValidation
.refDate
= new Date(dateVal
* 1000);
669 FormEngineValidation
.lastTime
= dateVal
+ FormEngineValidation
.parseTime(value
.substr(0,index
), value
.substr(0, 1), 'time');
671 // only date, no time
672 FormEngineValidation
.lastTime
= FormEngineValidation
.parseDate(value
, value
.substr(0, 1));
675 FormEngineValidation
.lastTime
+= add
* 24 * 60 * 60;
676 return FormEngineValidation
.lastTime
;
682 * @param {String} value
683 * @param {String} command
686 FormEngineValidation
.parseDate = function(value
, command
) {
687 var today
= new Date();
688 var values
= FormEngineValidation
.split(value
);
694 FormEngineValidation
.lastDate
= FormEngineValidation
.getTimestamp(today
);
695 if (values
.valPol
[1]) {
696 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
701 if (values
.valPol
[1]) {
702 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
707 if (values
.valPol
[index
]) {
708 add
= FormEngineValidation
.pol(values
.valPol
[index
], FormEngineValidation
.parseInt(values
.values
[index
]));
710 if (values
.values
[1] && values
.values
[1].length
> 2) {
711 if (values
.valPol
[2]) {
712 add
= FormEngineValidation
.pol(values
.valPol
[2], FormEngineValidation
.parseInt(values
.values
[2]));
714 var temp
= values
.values
[1];
715 values
= FormEngineValidation
.splitSingle(temp
);
718 var year
= (values
.values
[3]) ? FormEngineValidation
.parseInt(values
.values
[3]) : FormEngineValidation
.getYear(today
);
719 if ((year
>= 0 && year
< 38) || (year
>= 70 && year
< 100) || (year
>= 1902 && year
< 2038)) {
721 year
= (year
< 38) ? year
+= 2000 : year
+= 1900;
724 year
= FormEngineValidation
.getYear(today
);
726 var usMode
= FormEngineValidation
.USmode
? 1 : 2;
727 var month
= (values
.values
[usMode
]) ? FormEngineValidation
.parseInt(values
.values
[usMode
]) : today
.getUTCMonth() + 1;
728 usMode
= FormEngineValidation
.USmode
? 2 : 1;
729 var day
= (values
.values
[usMode
]) ? FormEngineValidation
.parseInt(values
.values
[usMode
]) : today
.getUTCDate();
731 var theTime
= new Date(parseInt(year
), parseInt(month
)-1, parseInt(day
));
733 // Substract timezone offset from client
734 FormEngineValidation
.lastDate
= FormEngineValidation
.convertClientTimestampToUTC(FormEngineValidation
.getTimestamp(theTime
), 0);
736 FormEngineValidation
.lastDate
+= add
* 24 * 60 * 60;
737 return FormEngineValidation
.lastDate
;
743 * @param {String} value
744 * @param {String} command
745 * @param {String} type
748 FormEngineValidation
.parseTime = function(value
, command
, type
) {
749 var today
= new Date();
750 var values
= FormEngineValidation
.split(value
);
756 FormEngineValidation
.lastTime
= FormEngineValidation
.getTimeSecs(today
);
757 if (values
.valPol
[1]) {
758 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
763 if (FormEngineValidation
.lastTime
== 0) {
764 FormEngineValidation
.lastTime
= FormEngineValidation
.getTimeSecs(today
);
766 if (values
.valPol
[1]) {
767 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
771 var index
= (type
== 'timesec') ? 4 : 3;
772 if (values
.valPol
[index
]) {
773 add
= FormEngineValidation
.pol(values
.valPol
[index
], FormEngineValidation
.parseInt(values
.values
[index
]));
775 if (values
.values
[1] && values
.values
[1].length
> 2) {
776 if (values
.valPol
[2]) {
777 add
= FormEngineValidation
.pol(values
.valPol
[2], FormEngineValidation
.parseInt(values
.values
[2]));
779 var temp
= values
.values
[1];
780 values
= FormEngineValidation
.splitSingle(temp
);
782 var sec
= (values
.values
[3]) ? FormEngineValidation
.parseInt(values
.values
[3]) : today
.getUTCSeconds();
786 var min
= (values
.values
[2]) ? FormEngineValidation
.parseInt(values
.values
[2]) : today
.getUTCMinutes();
790 var hour
= (values
.values
[1]) ? FormEngineValidation
.parseInt(values
.values
[1]) : today
.getUTCHours();
795 var theTime
= new Date(FormEngineValidation
.getYear(FormEngineValidation
.refDate
), FormEngineValidation
.refDate
.getUTCMonth(), FormEngineValidation
.refDate
.getUTCDate(), hour
, min
, (( type
== 'timesec' ) ? sec
: 0));
797 // Substract timezone offset from client
798 FormEngineValidation
.lastTime
= FormEngineValidation
.convertClientTimestampToUTC(FormEngineValidation
.getTimestamp(theTime
), 1);
800 FormEngineValidation
.lastTime
+= add
* 60;
801 if (FormEngineValidation
.lastTime
< 0) {
802 FormEngineValidation
.lastTime
+= 24 * 60 * 60;
804 return FormEngineValidation
.lastTime
;
810 * @param {String} value
811 * @param {String} command
814 FormEngineValidation
.parseYear = function(value
, command
) {
815 var today
= new Date();
816 var values
= FormEngineValidation
.split(value
);
822 FormEngineValidation
.lastYear
= FormEngineValidation
.getYear(today
);
823 if (values
.valPol
[1]) {
824 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
829 if (values
.valPol
[1]) {
830 add
= FormEngineValidation
.pol(values
.valPol
[1], FormEngineValidation
.parseInt(values
.values
[1]));
834 if (values
.valPol
[2]) {
835 add
= FormEngineValidation
.pol(values
.valPol
[2], FormEngineValidation
.parseInt(values
.values
[2]));
837 var year
= (values
.values
[1]) ? FormEngineValidation
.parseInt(values
.values
[1]) : FormEngineValidation
.getYear(today
);
838 if ((year
>= 0 && year
< 38) || (year
>= 70 && year
<100) || (year
>= 1902 && year
< 2038)) {
840 year
= (year
< 38) ? year
+= 2000 : year
+= 1900;
843 year
= FormEngineValidation
.getYear(today
);
845 FormEngineValidation
.lastYear
= year
;
847 FormEngineValidation
.lastYear
+= add
;
848 return FormEngineValidation
.lastYear
;
852 * Get year from date object
854 * @param {Date} timeObj
857 FormEngineValidation
.getYear = function(timeObj
) {
858 if (timeObj
=== null) {
861 return timeObj
.getUTCFullYear();
865 * Get date as timestamp from Date object
867 * @param {Date} timeObj
870 FormEngineValidation
.getDate = function(timeObj
) {
871 var theTime
= new Date(FormEngineValidation
.getYear(timeObj
), timeObj
.getUTCMonth(), timeObj
.getUTCDate());
872 return FormEngineValidation
.getTimestamp(theTime
);
877 * @param {String} foreign
878 * @param {String} value
881 FormEngineValidation
.pol = function(foreign
, value
) {
882 return eval(((foreign
== '-') ? '-' : '') + value
);
886 * Substract timezone offset from client to a timestamp to get UTC-timestamp to be send to server
888 * @param {Number} timestamp
889 * @param {Number} timeonly
892 FormEngineValidation
.convertClientTimestampToUTC = function(timestamp
, timeonly
) {
893 var timeObj
= new Date(timestamp
*1000);
894 timeObj
.setTime((timestamp
- timeObj
.getTimezoneOffset()*60)*1000);
896 // only seconds since midnight
897 return FormEngineValidation
.getTime(timeObj
);
899 // seconds since the "unix-epoch"
900 return FormEngineValidation
.getTimestamp(timeObj
);
905 * Parse date string or object and return unix timestamp
907 * @param {(String|Date)} timeObj
910 FormEngineValidation
.getTimestamp = function(timeObj
) {
911 return Date
.parse(timeObj
)/1000;
915 * Seconds since midnight
920 FormEngineValidation
.getTime = function(timeObj
) {
921 return timeObj
.getUTCHours() * 60 * 60 + timeObj
.getUTCMinutes() * 60 + FormEngineValidation
.getSecs(timeObj
);
929 FormEngineValidation
.getSecs = function(timeObj
) {
930 return timeObj
.getUTCSeconds();
938 FormEngineValidation
.getTimeSecs = function(timeObj
) {
939 return timeObj
.getHours() * 60 * 60 + timeObj
.getMinutes() * 60 + timeObj
.getSeconds();
943 * Find tab by field and mark it as has-validation-error
945 * @param {Object} $element
947 FormEngineValidation
.markParentTab = function($element
) {
948 var $panes
= $element
.parents('.tab-pane');
949 $panes
.each(function() {
951 var id
= $pane
.attr('id');
953 .find('a[href="#' + id
+ '"]')
954 .closest('.t3js-tabmenu-item')
955 .addClass('has-validation-error');
962 * @returns {{values: Array, pointer: number}}
964 FormEngineValidation
.splitSingle = function(value
) {
965 var theVal
= '' + value
;
970 result
.values
[1] = theVal
.substr(0,2);
971 result
.values
[2] = theVal
.substr(2,2);
972 result
.values
[3] = theVal
.substr(4,10);
983 FormEngineValidation
.splitStr = function(theStr1
, delim
, index
) {
984 var theStr
= '' + theStr1
;
985 var lengthOfDelim
= delim
.length
;
986 var sPos
= -lengthOfDelim
;
990 for (var a
= 1; a
< index
; a
++) {
991 sPos
= theStr
.indexOf(delim
, sPos
+ lengthOfDelim
);
996 var ePos
= theStr
.indexOf(delim
, sPos
+ lengthOfDelim
);
998 ePos
= theStr
.length
;
1000 return (theStr
.substring(sPos
+ lengthOfDelim
, ePos
));
1006 * @returns {{values: Array, valPol: Array, pointer: number, numberMode: number, theVal: string}}
1008 FormEngineValidation
.split = function(value
) {
1017 for (var a
=0; a
< value
.length
; a
++) {
1018 var theChar
= value
.substr(a
, 1);
1019 if (theChar
< '0' || theChar
> '9') {
1020 if (result
.numberMode
) {
1022 result
.values
[result
.pointer
] = result
.theVal
;
1024 result
.numberMode
= 0;
1026 if (theChar
== '+' || theChar
== '-') {
1027 result
.valPol
[result
.pointer
+ 1] = theChar
;
1030 result
.theVal
+= theChar
;
1031 result
.numberMode
= 1;
1037 FormEngineValidation
.registerReady = function() {
1038 FormEngineValidation
.initialize();
1041 FormEngine
.Validation
= FormEngineValidation
;
1043 return FormEngine
.Validation
;