$(document).ready(function () { $.validator.addMethod("validateNoFuture", (function(value, element, otherInputId) { var today = new Date(); var birthday = new Date(value); return birthday < today; })); $.validator.addMethod("validateNoFutureYear", (function(value,element, otherInputId) { var today = new Date(); var year = today.getFullYear(); var patt = /^(19|20)\d\d$/i; if (!patt.test(value)) { return false; } else { return value <= year; } })); $.validator.addMethod("requiredIf", (function(value,element, otherInputId) { if ( $('#giveDetails').is(':checked') && value == '' ){ return false; } return true; })); $.validator.addMethod('agreeTerms', function (value, element) { return $('#agreeTerms').is(':checked') ? true : false; }); // $.validator.addMethod('giveDetails', function (value, element) { // return $('#identity-yes').is(':checked') ? true : false; // }); function getDateFromFields(dayId, monthId, yearId) { var separator = "-"; var dayValue = $("#" + dayId + "").val(); var monthValue = $("#" + monthId + "").val(); var yearValue = $("#" + yearId + "").val(); if (dayValue === "" || monthValue === "" || yearValue === "") { return ""; } else { return yearValue + separator + monthValue + separator + dayValue; } }; var validateConfiguration = { ignore : ".ignore-js-validation", highlight : function(element, errorClass, validClass) { return $(element).closest(".control-group").removeClass("success").addClass("error"); }, unhighlight : function(element, errorClass, validClass) { if (element.id == "day" || element.id == "month" ){ return false; } return $(element).closest(".control-group").removeClass("error").addClass("success"); }, success : function(label) { return $(label).closest("form").find(".valid").removeClass("invalid"); }, errorPlacement : function(error, element) { if ((element[0].id == "day" || element[0].id == "month") && error.text() == "") return false; return element.closest("div").find(".error-wrapper").html(error.text()); }, onfocusout: function (element, errorClass, validClass) { if (element.id == "day" || element.id == "month" || element.id == "year"){ return; } $(element).valid(); if (element.id == "questionDetails") { $("#whistleblowingQuestion").val($("#questionDetails").val()); $("#whistleblowingQuestion").valid(); } if (element.id == "year") { $("#day").valid(); $("#month").valid(); } } }; /* $("#question").on("keydown", function() { $("#whistleblowingQuestion").val($("#questionDetails").val()); }); */ $("#whistleblowingForm").validate(validateConfiguration); $("#submitFormBtn").on('click',function(event) { // event.preventDefault(); if ( $('#giveDetails').is(':checked') ){ $("#showDetails").val('yes'); }else{ $("#showDetails").val('no'); } $("#whistleblowingDate").val(getDateFromFields('day', 'month', 'year')); $("#whistleblowingQuestion").val($("#questionDetails").val()); // if ( $("#whistleblowingDate").val() != '' ){ // this.submit(); // } }); if ( $("#showDetails").val() == 'yes' ){ // console.log($("#showDetails").val()); $("#giveDetails").prop('checked', true); $('.personal-info').removeClass('hidden'); }else{ // console.log($("#showDetails").val()); $("#identity-no").prop('checked', true); $('.personal-info').addClass('hidden'); } function setFieldsFromDate(date) { var year = date.slice(0,4); var month = date.slice(5, 7); var day = date.slice(-2); $("#day").val(day); $("#month").val(month); $("#year").val(year); }; if ( $("#whistleblowingDate").val() != '' ){ setFieldsFromDate( $("#whistleblowingDate").val() ); } var insertOnlyNumber = function(e){ var charCode = (typeof e.which === "number") ? e.which : e.keyCode, chr = String.fromCharCode(charCode); //convert it to a character if (e.keyCode == 46 || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 27 || (e.keyCode == 65 && e.ctrlKey === true) || (e.keyCode >= 35 && e.keyCode <= 39)) { return; } if(isNaN(parseInt(chr))) e.preventDefault(); }; $(".onlyNumbers").on('keypress', function(event) { insertOnlyNumber(event); }); if ($('.autocomplete').length === 0){ $('.firstName').val(''); $('.lastName').val(''); $('.email').val(''); $('.publication').val(''); $('.subject').val(''); $('.phoneNo').val(''); $('.country').val(''); $('.city').val(''); $('.attachmentFile').val(''); $('.questionDetails').val(''); $('.agreeTerms').val(''); } });