2
\$\begingroup\$

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);
 }
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 22, 2011 at 15:26
\$\endgroup\$
1
  • \$\begingroup\$ You can remove every 'return false;' and only put it at the end of each method. You could also extract the error message operations to one single method, with the message as parameter. \$\endgroup\$ Commented Nov 23, 2011 at 12:28

1 Answer 1

1
\$\begingroup\$

I would refactor conceptually and introduce a reusable TimeRange object with methods to check for inclusion:

class TimeRange : Range<DateTime>{
 bool Includes(DateTime other){...}
 bool Includes(TimeRange other){...}
 bool Includes(string other){...}
}

In this case, if Includes returns false, you have a validation error.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Nov 24, 2011 at 13:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.