0

I have start date and end date picker in format of mm-dd-yyyy.End date should be with in 10 days range of start date and end date should not be before start date. How to validate using angular js.Any help is greatly appreciated

3 Answers 3

1

I advise you to use moment.js. Here's a basic tutorial and help to get started: https://dzone.com/articles/getting-started-with-momentjs-intro-with-examples

Basically you would do:

function testDate(startDate, endDate){
    var start =  moment(startDate, 'mm-dd-yyyy');
    var end = moment(endDate, 'mm-dd-yyyy');
    if(endDate.isBefore(start)){
       //start before end
    }
    if(startDate.add('days', 11).isAfter(endDate)){
       //end not within 10 days range
    }
    //success!
}

It would be something like that. Hope it helps!

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

2 Comments

I need with out using moment.js
I do not want to use moment.js.That is not permitted due to some reasons
0

For simple pattern matching, you can use the ngPattern directive. For more complex validation (such as ensuring it's a valid date and within a certain range of another date), you will need to write your own validation directive or use the handy custom validation directive available from Angular-UI.

https://htmlpreview.github.io/?https://github.com/angular-ui/ui-validate/master/demo/index.html

Comments

0

Can you provide more details about this as in what library are you using for datepicker. It would help us answer.

Since you are using a datepicker you can restrict the date range in the fromDate field do not display any dates which are after a certain period (10 days from start date in you case).

jQuery datepicker allows this so does other datepickers.

EDIT: In your controller do something like this.

var newDate = new Date($scope.startdate.getFullYear(), $scope.startdate.getMonth(), $scope.startdate.getDate()+10);
$scope.dateOptions = {
 maxDate: newDate,
 dateFormat: 'dd/mm/YYYY'
};

And now you can pass dateOptions to your input.

Note: Post a minimal plunkr for faster resolution of your issues.

11 Comments

I am using <input type=“text” ui-date ui-date-format ng-model=“startdate” ></input> .I am using same for end date date picker with different model name.Could you please help validating max date should be 10 days from start date
How to pass date options to input?I am new to angular js?Could you provide sample code?
try <input type=“text” ui-date="dateOptions" ng-model=“startdate”/>
I got an error TypeError:Cannot read property ‘getFullYear’ of undefined.
Can you post your code in a plunkr so that its easier for me to see what's going on? you can find a reference plunkr here
|

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.