jQuery.validator.addMethod("dateDMY", function (value, element) { var check = false; var re = /^(0[1-9]|[12][0-9]|3[01])[\.](0[1-9]|1[012])[\.]\d{4}$/; if (re.test(value)) { var adata = value.split("."); var day = parseInt(adata[0], 10); var month = parseInt(adata[1], 10); var year = parseInt(adata[2], 10); if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) { check = false; // 31st of a month with 30 days } else if (day >= 30 && month == 2) { check = false; // February 30th or 31st } else if (month == 2 && day == 29 && !(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) { check = false; // February 29th outside a leap year } else { check = true; // Valid date } } // the calendar does not allow to clear values if it is required (js). So the required option in this validation is just a workaround fallback if (value == "0000-00-00 00:00:00" && !jQuery(element).prop("required")) { check = true; } return this.optional(element) || check; }); jQuery.validator.addMethod("dateMDY", function (value, element) { var check = false; var re = /^(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/]\d{4}$/; if (re.test(value)) { var adata = value.split("/"); var month = parseInt(adata[0], 10); var day = parseInt(adata[1], 10); var year = parseInt(adata[2], 10); if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) { check = false; // 31st of a month with 30 days } else if (day >= 30 && month == 2) { check = false; // February 30th or 31st } else if (month == 2 && day == 29 && !(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) { check = false; // February 29th outside a leap year } else { check = true; // Valid date } } // the calendar does not allow to clear values if it is required (js). So the required option in this validation is just a workaround fallback if (value == "0000-00-00 00:00:00" && !jQuery(element).prop("required")) { check = true; } return this.optional(element) || check; }); jQuery.validator.addMethod("dateYMD", function (value, element) { var check = false; var re = /^\d{4}[\-](0[1-9]|1[012])[\-](0[1-9]|[12][0-9]|3[01])$/; if (re.test(value)) { var adata = value.split("-"); var year = parseInt(adata[0], 10); var month = parseInt(adata[1], 10); var day = parseInt(adata[2], 10); if (day == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) { check = false; // 31st of a month with 30 days } else if (day >= 30 && month == 2) { check = false; // February 30th or 31st } else if (month == 2 && day == 29 && !(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))) { check = false; // February 29th outside a leap year } else { check = true; // Valid date } } // the calendar does not allow to clear values if it is required (js). So the required option in this validation is just a workaround fallback if (value == "0000-00-00 00:00:00" && !jQuery(element).prop("required")) { check = true; } return this.optional(element) || check; }); jQuery.validator.addMethod("filesize", function (value, element, maxsize) { var check = false; if ((maxsize === 0) || ((!(element.files.length == 0)) && (element.files[0].size < maxsize))) { check = true; } return this.optional(element) || check; }); jQuery.validator.addMethod("fileextension", function (value, element, allowedextension) { var check = false; allowedextension = allowedextension.replace(/\s/g, ""); allowedextension = allowedextension.split(","); var fileext = jQuery(element).val().split(".").pop().toLowerCase(); if (jQuery.inArray(fileext, allowedextension) > -1) { check = true; } return this.optional(element) || check; }); jQuery.validator.addMethod("filename", function (value, element) { const invalidRegex = /^\.|\.{2,}|[^A-Za-z0-9._ -]/; // returns true, if the file name contains invalid characters return this.optional(element) || !invalidRegex.test(value); }); jQuery.validator.addMethod("customvalidation", function (value, element, re) { re = new RegExp(re); return this.optional(element) || re.test(value); }); jQuery.validator.addMethod("ispair", function (value, element, id) { var latval = document.getElementById(id+"_lat").value; var lngval = document.getElementById(id+"_lng").value; // false if on field is empty and the other not var check = ((latval === "" && lngval === "") || (latval !== "" && lngval !== "")); var relatval = /^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/; var relngval = /^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/; check = (latval === "" || relatval.test(latval)) && check; check = (lngval === "" || relngval.test(lngval)) && check; return check; }); jQuery.validator.addMethod("mindate", function(value, element, options) { var check = false; var minDate = ""; if (value) { if (options.fromField) { var fieldId = options.value; var field = document.getElementById(fieldId); if (!field) { return true; } if (field.disabled) { return true; } minDate = field.value; if (!minDate) { return true; } } else { minDate = options.value; } var format, i = 0, fmt = {}, minDateFormat, j = 0, minDateFmt = {}, day; format = (value.indexOf(".") > -1) ? "dd.mm.yyyy" : ((value.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; }); minDateFormat = (minDate.indexOf(".") > -1) ? "dd.mm.yyyy" : ((minDate.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); minDateFormat.replace(/(yyyy|dd|mm)/g, function(part) { minDateFmt[part] = j++; }); var minDateParts = minDate.match(/(\d+)/g); var valueParts = value.match(/(\d+)/g); minDate = new Date(minDateParts[minDateFmt["yyyy"]], minDateParts[minDateFmt["mm"]]-1, minDateParts[minDateFmt["dd"]],0,0,0,0); if (options.shift) { var shift = options.shift; day = minDate.getDate(); day = day + parseInt(shift); minDate.setDate(day); } value = new Date(valueParts[fmt["yyyy"]], valueParts[fmt["mm"]]-1, valueParts[fmt["dd"]],0,0,0,0); check = value >= minDate; } return this.optional(element) || check; }, function(options, element) { // validation message if (options.fromField) { var minDate = ""; var fieldId = options.value; var field = document.getElementById(fieldId); if (field) { minDate = field.value; } } else { minDate = options.value; } var format, minDateFormat, j = 0, minDateFmt = {}, day, month, year, valDate; minDateFormat = (minDate.indexOf(".") > -1) ? "dd.mm.yyyy" : ((minDate.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); minDateFormat.replace(/(yyyy|dd|mm)/g, function(part) { minDateFmt[part] = j++; }); var minDateParts = minDate.match(/(\d+)/g); minDate = new Date(minDateParts[minDateFmt["yyyy"]], minDateParts[minDateFmt["mm"]]-1, minDateParts[minDateFmt["dd"]],0,0,0,0); if (options.shift) { var shift = options.shift; day = minDate.getDate(); day = day + parseInt(shift); minDate.setDate(day); } format = options.format; valDate = ""; day = minDate.getDate(); if (day < 10) { day = "0" + day; } month = 1 + minDate.getMonth(); if (month < 10) { month = "0" + month; } year = minDate.getFullYear(); switch (format) { case "%Y-%m-%d" : valDate = year + "-" + month + "-" + day; break; case "%m/%d/%Y" : valDate = month + "/" + day + "/" + year; break; default : valDate = day + "." + month + "." + year; break; } return jQuery.validator.format(Joomla.Text._( 'COM_VISFORMS_MINDATE_VALIDATION_FAILED_JS'), valDate); }); jQuery.validator.addMethod("maxdate", function(value, element, options) { var check = false; var minDate = ""; if (value) { if (options.fromField) { var fieldId = options.value; var field = document.getElementById(fieldId); if (!field) { return true; } if (field.disabled) { return true; } minDate = field.value; if (!minDate) { return true; } } else { minDate = options.value; } var format, i = 0, fmt = {}, minDateFormat, j = 0, minDateFmt = {}, day; format = (value.indexOf(".") > -1) ? "dd.mm.yyyy" : ((value.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; }); minDateFormat = (minDate.indexOf(".") > -1) ? "dd.mm.yyyy" : ((minDate.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); minDateFormat.replace(/(yyyy|dd|mm)/g, function(part) { minDateFmt[part] = j++; }); var minDateParts = minDate.match(/(\d+)/g); var valueParts = value.match(/(\d+)/g); minDate = new Date(minDateParts[minDateFmt["yyyy"]], minDateParts[minDateFmt["mm"]]-1, minDateParts[minDateFmt["dd"]],0,0,0,0); if (options.shift) { var shift = options.shift; day = minDate.getDate(); day = day + parseInt(shift); minDate.setDate(day); } value = new Date(valueParts[fmt["yyyy"]], valueParts[fmt["mm"]]-1, valueParts[fmt["dd"]],0,0,0,0); check = value <= minDate; } return this.optional(element) || check; }, function(options, element) { // validation message if (options.fromField) { var minDate = ""; var fieldId = options.value; var field = document.getElementById(fieldId); if (field) { minDate = field.value; } } else { minDate = options.value; } var format, minDateFormat, j = 0, minDateFmt = {}, day, month, year, valDate; minDateFormat = (minDate.indexOf(".") > -1) ? "dd.mm.yyyy" : ((minDate.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); minDateFormat.replace(/(yyyy|dd|mm)/g, function(part) { minDateFmt[part] = j++; }); var minDateParts = minDate.match(/(\d+)/g); minDate = new Date(minDateParts[minDateFmt["yyyy"]], minDateParts[minDateFmt["mm"]]-1, minDateParts[minDateFmt["dd"]],0,0,0,0); if (options.shift) { var shift = options.shift; day = minDate.getDate(); day = day + parseInt(shift); minDate.setDate(day); } format = options.format; valDate = ""; day = minDate.getDate(); if (day < 10) { day = "0" + day; } month = 1 + minDate.getMonth(); if (month < 10) { month = "0" + month; } year = minDate.getFullYear(); switch (format) { case "%Y-%m-%d" : valDate = year + "-" + month + "-" + day; break; case "%m/%d/%Y" : valDate = month + "/" + day + "/" + year; break; default : valDate = day + "." + month + "." + year; break; } return jQuery.validator.format(Joomla.Text._( 'COM_VISFORMS_MAXDATE_VALIDATION_FAILED_JS'), valDate); }); jQuery.validator.addMethod("minage", function (value, element, options) { let check = false, minage = "", age = "", format, i = 0, fmt = {}, valueParts, years, now = new Date(); // a date is selected if (value) { // no minage set if (options.minage) { minage = options.minage; } else { return true; } // get year, month and day from selected date // with regards to the currently used format format = (value.indexOf(".") > -1) ? "dd.mm.yyyy" : ((value.indexOf("/") > -1) ? "mm/dd/yyyy" : "yyyy-mm-dd"); format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; }); valueParts = value.match(/(\d+)/g); value = new Date(valueParts[fmt["yyyy"]], valueParts[fmt["mm"]]-1, valueParts[fmt["dd"]],0,0,0,0); // get the difference between the year of now and the selected date years = now.getFullYear() - value.getFullYear(); // set year in selected date to current year value.setFullYear(value.getFullYear() + years); // if the selected date is then in the future, substact 1 from years, because the last year is not yet completed if (value > now) { years--; } check = years >= minage; } return this.optional(element) || check; }); jQuery.validator.addMethod("phonevalidation", function (value, element, options) { let check = false, format, regex; // a phonenumber is given if (value) { // get validation type if (options.phonevalidation) { format = options.phonevalidation; } else { return true; } if (options.purge) { options.purge.forEach((element) => { if (element === "BLANK") { value = value.replaceAll(" ", ""); } if (element === "BRACKET") { value = value.replaceAll("(", ""); value = value.replaceAll(")", ""); } if (element === "POINT") { value = value.replaceAll(".", ""); } if (element === "SLASH") { value = value.replaceAll("/", ""); } if (element === "MINUS") { if (!(format === "NANP")) { value = value.replaceAll("-", ""); } } if (element === "PLUS") { if (!(format === "ITU-T")) { value = value.replaceAll("+", ""); } } }); } switch (format) { case "NANP" : regex = /^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/; break; case "ITU-T" : regex = /^\+(?:[0-9] ?){6,14}[0-9]$/; break; case "EPP" : regex = /^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/; break; case "GERMANY": regex = /^((\+[1-9]{1}\d{1,4}) ?|0)[1-9]{1}\d{1,3} ?[1-9]{1}\d{3,12}(-\d+)?$/; break; case "NATIONAL": regex = /^\d+$/; break; default : return true; } check = (value === "" || regex.test(value)); } return this.optional(element) || check; }); jQuery.validator.addMethod("mincalvalue", function (value, element, options) { let check = value.replace(",", "."); return this.optional( element ) || check >= options; }); jQuery.validator.addMethod("maxcalvalue", function (value, element, options) { let check = value.replace(",", "."); return this.optional( element ) || check <= options; }); jQuery.validator.addMethod("requiredwithinclude", function (value, element, options) { if (value.length > 0) { if (options.purge) { options.purge.forEach((element) => { if (element === "BLANK") { value = value.replaceAll(" ", ""); } if (element === "BRACKET") { value = value.replaceAll("(", ""); value = value.replaceAll(")", ""); } if (element === "POINT") { value = value.replaceAll(".", ""); } if (element === "SLASH") { value = value.replaceAll("/", ""); } if (element === "MINUS") { value = value.replaceAll("-", ""); } if (element === "PLUS") { value = value.replaceAll("+", ""); } }); } } return value.length > 0; });