I'm new to Angular and struggling with date handling. I have a form
that includes fields for the user's name
and their date of birth
.
Before submitting the form, I need to validate that the person is over 18 years old and display an error message if they are not. However, I can't access the dob
variable in the form
until it is submitted. The only place I have access to $scope.contact.dob
is inside the addContact()
function after the submission. How can I validate the date of birth before form submission?
Here's a snippet of my code:
<form>
<input ng-model="contact.name">
<input ng-model="contact.dob" ng-min="minAge()">
<p ng-show="userForm.birthday.$error.min">Must be at least 18.</p>
<button ng-click=addContact()></button>
</form>
Here's my controller:
.controller("myctrl", function($scope){
$scope.addContact = fucntion(){
console.log($scope.contact)
$scope.contact.dob
}
$scope.minAge = function () {
var current = new Date();
var minYear = current.getFullYear() - 100;
var min = new Date(minYear,current.getMonth(),current.getDate()).toISOString();
return min;
};
})