Extend the JS validation to support dd/mm/YYYY format (#1418)
[openemr.git] / library / js / vendors / validate / validate_extend.js
blob332923a2aaf5f7cbc746afcea81887157104817a
1 /**
2  * LICENSE: This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 3
5  * of the License, or (at your option) any later version.
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with this program. If not, see
12  * http://www.gnu.org/licenses/licenses.html#GPL .
13  *
14  * @package OpenEMR
15  * @author  Sharon Cohen <sharonco@matrix.co.il>
16  * @author  Amiel Elboim <amielel@matrix.co.il>
17  * @link    http://www.open-emr.org
18  */
20 // Extend for date roll from  https://validatejs.org/#validators-datetime
22 // Before using it we must add the parse and format functions
23 // Here is a sample implementation using moment.js
24 validate.extend(validate.validators.datetime, {
25     // The value is guaranteed not to be null or undefined but otherwise it
26     // could be anything.
27     parse: function(value, options) {
28         var format = (typeof options.format !== 'undefined') ? options.format : 'YYYY-MM-DD';
29         return (moment.utc(value, format));
30     },
31     // Input is a unix timestamp
32     format: function(value, options) {
33         var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss";
34         return moment.utc(value).format(format);
35     }
36 });
40 *  Custom validator documentation - https://validatejs.org/#custom-validator
43 /**
44 * validate that date is past date, recommended to put it after {date: {dateOnly: true}}
45 * you can specify the message option {onlyPast:{message:'text example'}}
46 * optional options -
47 * 1)(string) massage
48 * 2)(boolean) onlyYear
50 validate.validators.pastDate = function(value, options) {
51     //exit if empty value
52     if(validate.isEmpty(value)) { return;}
53     // exit if options = false
54     if(!options) return;
55     //Without this fix an empty date doesn't pass validation because value in DB is "0000-00-00".
56     if(value == "0000-00-00"){
57         return;
58     }
59     var now = new Date().getTime();
60     //if set onlyYear option
61     if (options.onlyYear != undefined && options.onlyYear) {
62         if(value < 1800 || value > now.getFullYear ) {
64             return throwError('Must be year format');
65         }
66     }
68     var format=0;
69     if (typeof(g_date_format) !== 'undefined') {
70         format=g_date_format;
71     }
73     var date = '';
75         switch(format) {
76             // case date format is dd/mm/YYYY
77             case "2":
78                 var dateParts = value.split("/");
79                 var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
80                 break;
81             default:
82                 date =  new Date(value);
83         }
86     var mls_date = date.getTime();
87     if(isNaN(mls_date)) {
88        return throwError('Must be valid date');
89     }
91     if(now < mls_date) {
93        return throwError( 'Must be past date');
94     }
96     // throw error
97     function throwError(message){
98         if(validate.isObject(options) && options.message != undefined) {
99             return options.message;
100         } else {
101             return message;
102         }
103     }
108  * Luhn algorithm in JavaScript: validate credit card number supplied as string of numbers
109  * @author ShirtlessKirk. https://gist.github.com/ShirtlessKirk/2134376
110  * you can specify the message option {luhn:{message:'text example'}}
112  */
114 validate.validators.luhn = function(value, options) {
116     //calculate Luhn algorithm
117     var luhnChk = (function (arr) {
118         return function (ccNum) {
119             var
120                 len = ccNum.length,
121                 bit = 1,
122                 sum = 0,
123                 val;
125             while (len) {
126                 val = parseInt(ccNum.charAt(--len), 10);
127                 sum += (bit ^= 1) ? arr[val] : val;
128             }
130             return sum && sum % 10 === 0;
131         };
132     }([0, 2, 4, 6, 8, 1, 3, 5, 7, 9]));
134     //exit if empty value
135     if(validate.isEmpty(value)) { return; }
136     // exit if options = false
137     if(!options) return;
139     var valid = luhnChk(value);
141     if(!valid) {
142         if(validate.isObject(options) && options.message != undefined) {
143             return options.message;
144         } else {
145             return 'Invalid luhn algorithm';
146         }
147     }