Commit dd57c8b5 by muzaffaryousaf

More accurate validation for date & time in studio editor.

TNL-58
parent 51df3aa6
......@@ -44,6 +44,10 @@ describe("OpenAssessment.DatetimeControl", function() {
testValidateDate(datetimeControl, "99999999-01-01", "00:00", false, expectedError);
testValidateDate(datetimeControl, "2014-99999-01", "00:00", false, expectedError);
testValidateDate(datetimeControl, "2014-01-99999", "00:00", false, expectedError);
//invalid month
testValidateDate(datetimeControl, "2014-13-01", "00:00", false, expectedError);
//invalid day
testValidateDate(datetimeControl, "2014-02-30", "00:00", false, expectedError);
});
it("validates invalid times", function() {
......@@ -64,6 +68,8 @@ describe("OpenAssessment.DatetimeControl", function() {
testValidateDate(datetimeControl, "2001-12-31", "00:00", true);
testValidateDate(datetimeControl, "2014-04-01", "12:34", true);
testValidateDate(datetimeControl, "2014-04-01", "23:59", true);
// single character for month and date.
testValidateDate(datetimeControl, "2014-4-1", "23:59", true);
});
it("clears validation errors", function() {
......
......@@ -227,16 +227,34 @@ OpenAssessment.DatetimeControl.prototype = {
**/
validate: function() {
var datetimeString = this.datetime();
var matches = datetimeString.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/g);
var isValid = (matches !== null);
var dateString = $(this.datePicker, this.element).val();
var timeString = $(this.timePicker, this.element).val();
if (!isValid) {
//date validation
var isDateValid = false;
//try: to catch the error thrown by parseDate if date is not in expected format.
try {
var parsedDate = $.datepicker.parseDate("yy-mm-dd", dateString);
if (parsedDate instanceof Date) {
isDateValid = true;
}
}
catch (err) {
// do nothing.
}
if (!isDateValid) {
$(this.datePicker, this.element).addClass("openassessment_highlighted_field");
}
//time validation
var matches = timeString.match(/^\d{2}:\d{2}$/g);
var isTimeValid = (matches !== null);
if(!isTimeValid) {
$(this.timePicker, this.element).addClass("openassessment_highlighted_field");
}
return isValid;
return (isDateValid && isTimeValid);
},
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment