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 .
15 * @author Sharon Cohen <sharonco@matrix.co.il>
16 * @author Amiel Elboim <amielel@matrix.co.il>
17 * @link http://www.open-emr.org
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
27 parse: function(value, options) {
28 var format = (typeof options.format !== 'undefined') ? options.format : 'YYYY-MM-DD';
29 return (moment.utc(value, format));
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);
40 * Custom validator documentation - https://validatejs.org/#custom-validator
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'}}
48 * 2)(boolean) onlyYear
50 validate.validators.pastDate = function(value, options) {
52 if(validate.isEmpty(value)) { return;}
53 // exit if options = false
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"){
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');
69 if (typeof(g_date_format) !== 'undefined') {
76 // case date format is dd/mm/YYYY
78 var dateParts = value.split("/");
79 var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
82 date = new Date(value);
86 var mls_date = date.getTime();
88 return throwError('Must be valid date');
93 return throwError( 'Must be past date');
97 function throwError(message){
98 if(validate.isObject(options) && options.message != undefined) {
99 return options.message;
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'}}
114 validate.validators.luhn = function(value, options) {
116 //calculate Luhn algorithm
117 var luhnChk = (function (arr) {
118 return function (ccNum) {
126 val = parseInt(ccNum.charAt(--len), 10);
127 sum += (bit ^= 1) ? arr[val] : val;
130 return sum && sum % 10 === 0;
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
139 var valid = luhnChk(value);
142 if(validate.isObject(options) && options.message != undefined) {
143 return options.message;
145 return 'Invalid luhn algorithm';