I am trying to validate time entry. ArrTime
is the time entered by the user from the UI. I am trying to make it so that when a user attempts to add a time, it must be between 8:45 and 17:30. Is there any thing wrong with my code? I am not very good at JavaScript so I have a feeling it is that, but if you can help me at all I will be very grateful.
Here are all the parts of code that are involved in validation:
JavaScript
timeValid: function () {
var s = $(this).val();
if ($(this).required()) {
var arrTime = new ArrTime(s);
var startTime = new Date(0001, 01, 01, 08, 46, 00, 00);
var endTime = new Date(9999, 12, 366, 17, 30, 00, 00);
if (arrTime > startTime) {
$(this).removeClass("error");
return true;
}
else {
validation.showError("Invalid", $(this).attr("name"));
$(this).addClass("error");
}
if (!isNaN(arrTime)) {
if (arrTime >= ArrTime.parse(new ArrTime().toTimeString())) {
$(this).removeClass("error");
return true;
}
else {
validation.showError("Invalid", $(this).attr("name"));
$(this).addClass("error");
return false;
}
}
else {
validation.showError("Required", $(this).attr("name"));
$(this).addClass("error");
return false;
}
}
else return false;
},
JavaScript
var deferrend = $.Deferred();
if (new Date(dateTo) > new Date()) {
deferrend = confirmation.ask("Are you sure you want to add a future absence?");
} else {
deferrend.resolveWith(true);
}
if (new Date(arrTime) > new Date()) {
deferrend = confirmation.ask("Are you sure this is the correct time for being late?");
}
else {
deferrend.resolveWith(true);
}
C#
public static bool IsValidTime(this string s, out string error)
{
error = String.Empty;
if (s.Required())
{
TimeSpan outTime;
TimeSpan startTime = new TimeSpan(8, 46, 0);
TimeSpan endTime = new TimeSpan(17, 30, 0);
if (TimeSpan.TryParse(s, out outTime))
if (outTime >= startTime && outTime <= endTime)
return true;
else
{
error = "Invalid";
return false;
}
}
else
{
error = "Required";
return false;
}
return false;
}
}
JavaScript
if (new Date(arrTime) > new Date()) {
deferrend = confirmation.ask("Are you sure this is the correct time for being late?");
}
else {
deferrend.resolveWith(true);
}