0

We are using Angular js for our project. And all the Validation we are handling through the Angular js.

We are able to validate all the text field except the drop down box.

We have three dropdown box for datefields(dd mm yyyy). Now i have two questions...

  1. HOw i can fetch values from these three dropdown and how to form a date.

  2. Then, i want to validate that date (if age is greater than 50 then set error flag). How i can do it using Angular js? I want to set Form as invalid if user select the age beyond 50.

Please suggest me with this? I'm completely new to angular js.

1
  • 2
    show some code of what you have Commented Aug 26, 2013 at 13:16

1 Answer 1

1

I don't have any code from you to base an answer off of, but here is a function to help you identify the correct age:

$scope.dateDiff = function(birthMonth, birthDay, birthYear) {
    var todayDate = new Date(),
        todayYear = todayDate.getFullYear(),
        todayMonth = todayDate.getMonth(),
        todayDay = todayDate.getDate(),
        age = todayYear - birthYear; 

    if (todayMonth < birthMonth - 1)
    {
      age--;
    }

    if (birthMonth - 1 === todayMonth && todayDay < birthDay)
    {
      age--;
    }
    return age;
};

$scope.dateDiff(8,15,1963); // return as 50 as birthday has passed
$scope.dateDiff(9,15,1963); // return as 49 as birthday has not yet happened

In your HTML you could use ng-options to allow the user to select dates from dropdown list.

Day <select ng-model="selectedDay" ng-options="day as day.date for day in days"></select>

Here is a full working example.

Being that you're new to Angular, make sure you take the time to learn how this code works. ng-options may seem a little confusing at first, but when you start to understand Angular Principles, it shouldn't be a problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.