Issue: jQuery validation is not working on Safari but it does on Google chrome, firefox.
The CMS that handles the form to sign up need the date in the following format: YYYY-MM-DD. How people are used to fill in the form is like: DD-MM-YYYY. So i order to manage that i used jQuery (https://jqueryui.com/datepicker/). I set the correct date using dateFormat and used the altFormat to change the appearance of the date. When submitting the form, the validator method: minAge16 should check if the user is 16 years or older. When i test it on Google Chrome or Firefix, it works fine and return the error message; you are not 16 years or older. But when i test it on Safari browser or iPhone, it keeps saying you are not 16 years or older. My guess it that it doesn't work since safari makes use of a different time format. Has anyone else faced the validation problem in Safari or Chrome and how did you manage to find a work around?
jQuery Datepicker:
$(document).ready(function() {
$( ".js-date-picker" ).datepicker({
dateFormat: "yy-mm-dd",
altFormat: "dd-mm-yy"
});
});
The form:
<form action="" method="post" class="text-center py-4 js-validate" id="signup">
<div class="row">
<div class="col-12 mb-2">
<label class="input">
<input type="text"
name="data[invoice][birthdate]"
placeholder="Date of birth*"
class="input__field js-date-picker mt-2" required value="">
</label>
</div>
</div>
<button type="submit" name="button" class="d-block mx-auto button mt-4 mt-lg-5">Submit</button>
Javascript:
$(document).ready(function() {
// Check if browser supports date input
var datePickerFormatMessage = '';
var inputField = document.createElement('input');
inputField.setAttribute('type', 'date');
if(inputField.type != 'date') {
datePickerFormatMessage = ' (JJJJ-MM-DD)';
}
// Check if the user entered a valid date
$.validator.addMethod('validDate', function(value, element) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
if(!value.match(regEx)) {
return false; // Invalid format
}
var today = new Date();
var date = new Date(value);
if(Number.isNaN(date.getTime())) {
return false; // Invalid date
} else if ((today.getFullYear() - date.getFullYear()) > 110) {
return false; // Invalid age
}
return date.toISOString().slice(0, 10) === value;
}, ('Please enter a valid date' + datePickerFormatMessage));
$.validator.addMethod('minAge16', function(value, element) {
var birthDate = new Date(value);
var eighteenYearsAgo = new Date();
eighteenYearsAgo.setFullYear(eighteenYearsAgo.getFullYear() - 16);
return (birthDate < eighteenYearsAgo);
}, 'You must be atleast 16 years or older.');
$('.js-validate').each(function() {
$(this).validate({
rules: {
'data[invoice][birthdate]': {
validDate: true,
minAge16: true
}
},
messages: {
'data[buyer][birthdate]': {
date: 'Fill in a valid date' + datePickerFormatMessage
}
},
});
});
});
Here it goes wrong:
$.validator.addMethod('minAge16', function(value, element) {
// here it goes wrong when i debug in safari, i am not getting a correct date back to check the difference
var birthDate = new Date(value);
var eighteenYearsAgo = new Date();
eighteenYearsAgo.setFullYear(eighteenYearsAgo.getFullYear() - 16);
return (birthDate < eighteenYearsAgo);
}, 'You must be atleast 16 years or older.');