Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5048 dpurdie 1
/**
2
 * jQuery Form Validator Module: Date
3
 * ------------------------------------------
4
 * Created by Victor Jonsson <http://www.victorjonsson.se>
5
 * Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
6
 *
7
 * The following validators will be added by this module:
8
 *  - Time (HH:mmm)
9
 *  - Birth date
10
 *
11
 * @website http://formvalidator.net/#location-validators
12
 * @license Dual licensed under the MIT or GPL Version 2 licenses
13
 * @version 2.2.beta.69
14
 */
15
(function($) {
16
 
17
    /*
18
     * Validate time hh:mm
19
     */
20
    $.formUtils.addValidator({
21
        name : 'time',
22
        validatorFunction : function(time) {
23
            if (time.match(/^(\d{2}):(\d{2})$/) === null) {
24
                return false;
25
            } else {
26
                var hours = parseInt(time.split(':')[0],10);
27
                var minutes = parseInt(time.split(':')[1],10);
28
                if( hours > 23 || minutes > 59 ) {
29
                    return false;
30
                }
31
            }
32
            return true;
33
        },
34
        errorMessage : '',
35
        errorMessageKey: 'badTime'
36
    });
37
 
38
    /*
39
     * Is this a valid birth date
40
     */
41
    $.formUtils.addValidator({
42
        name : 'birthdate',
43
        validatorFunction : function(val, $el, conf) {
44
            var dateFormat = 'yyyy-mm-dd';
45
            if($el.valAttr('format')) {
46
                dateFormat = $el.valAttr('format');
47
            }
48
            else if(typeof conf.dateFormat != 'undefined') {
49
                dateFormat = conf.dateFormat;
50
            }
51
 
52
            var inputDate = $.formUtils.parseDate(val, dateFormat);
53
            if (!inputDate) {
54
                return false;
55
            }
56
 
57
            var d = new Date();
58
            var currentYear = d.getFullYear();
59
            var year = inputDate[0];
60
            var month = inputDate[1];
61
            var day = inputDate[2];
62
 
63
            if (year === currentYear) {
64
                var currentMonth = d.getMonth() + 1;
65
                if (month === currentMonth) {
66
                    var currentDay = d.getDate();
67
                    return day <= currentDay;
68
                }
69
                else {
70
                    return month < currentMonth;
71
                }
72
            }
73
            else {
74
                return year < currentYear && year > (currentYear - 124); // we can not live for ever yet...
75
            }
76
        },
77
        errorMessage : '',
78
        errorMessageKey: 'badDate'
79
    });
80
 
81
})(jQuery);