I have some sort of spaghetti code in a file. The file itself contains the input field for the Date of Birth (DOB).
The script work fine on Chrome and IE11, however it doesn't work on Firefox.
There's a bug where you need to pass a second parameter to parseInt()
, but then I lose option validate the date. Therefore I need help to either cleanly parse numbers and put them together to a date-string.
I debugged it a little and found out that in FF there is no console log, whilst it clearly should do so. Basically, it seems that FF gets stuck somewhere, but I have no idea where and why.
Purpose of this script is to validate that the entered Values are either INT and combined verify the age of a customer (basically I want to check if he is 18 or not - based on 3 inputs, Day, Month, Year).
<script type="text/javascript">
//<![CDATA[
var customer_dob = new Varien.DOB('.customer-dob', <?php echo $this->isRequired() ? 'true' : 'false' ?>, '<?php echo $this->getDateFormat() ?>');
jQuery(document).ready(function($){
var sPath = window.location.pathname;
// OPTION 1: /customer/account/create/
// OPTION 2: /onestepcheckout/
// OPTION 3: /customer/account/edit/
console.log(sPath);
var age = [];
if(sPath == "/onestepcheckout/"){
var disButID = "#onestepcheckout-place-order";
} else if (sPath == "/customer/account/create/") {
var disButID = ".buttons-set button.button";
} else if (sPath == "/customer/account/edit/") {
var disButID = ".buttons-set button.button";
}
//console.log(disButID);
//jQuery(disButID).prop("disabled", true);
jQuery(".customer-dob input").each(function($){
var entity = jQuery(this).attr("name");
//console.log(entity);
if(entity == "day" || entity == "month" || entity == "year"){
var selector = jQuery(this).attr("data-selector");
age[selector] = jQuery(this).val();
getAge(age);
//console.log("Change check: " + age);
} else if (entity == "dob") {
// ...
}
});
jQuery(".customer-dob input").change(function($){
var selector = jQuery(this).attr("data-selector");
age[selector] = jQuery(this).val();
getAge(age);
//console.log("Change check: " + age);
});
function getAge(age) {
if(age["d"] && age["m"] && age["y"]){
var day = age["d"];
var month = age["m"];
var year = age["y"];
console.log("Date: " + day, month, year);
unlockOrderButton(day, month, year);
} else {
// ...
//console.log(age.length);
}
}
function unlockOrderButton(day, month, year){
var dateString = parseInt(month) + "-" + parseInt(day) + "-" + parseInt(year);
var today = new Date();
var birthDate = new Date(dateString);
var currentAge = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
currentAge--;
} else {
currentAge;
}
//var age = parseInt(curr);
console.log(parseInt(currentAge));
console.log("Unlock Button - Check");
if(currentAge >= 18){
jQuery(".customer-dob-error").removeClass("active");
jQuery(disButID).prop("disabled", false);
console.log("Unlock Button - Success. Age is: " + currentAge);
} else {
jQuery(".customer-dob-error").addClass("active");
jQuery(disButID).prop("disabled", true);
console.debug("Unlock Button - Fail. Age is: " + currentAge);
}
}
});
//]]>
</script>
HTML Code:
<div class="input-box customer-dob">
<div class="dob-day">
<input type="text" data-selector="d" id="billing:day" name="billing[day]" value="" title="Tag" class="input-text validate-custom">
<label for="billing:day">TT</label>
</div><div class="dob-month">
<input type="text" data-selector="m" id="billing:month" name="billing[month]" value="" title="Monat" class="input-text validate-custom">
<label for="billing:month">MM</label>
</div><div class="dob-year">
<input type="text" data-selector="y" id="billing:year" name="billing[year]" value="" title="Jahr" class="input-text validate-custom" autocomplete="off">
<label for="billing:year">JJJJ</label>
</div>
<div class="dob-full" style="display:none;">
<input type="hidden" id="billing:dob" name="billing[dob]">
</div>
<div class="validation-advice" style="display:none;"></div>
</div>
Firebug fails to show console's content, so I checked the built-in console.
Date: 17 02 1950 onestepcheckout:1073:5
Unlock Button - Check onestepcheckout:1096:4
Unlock Button - Fail. Age is: NaN onestepcheckout:1104:5