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);
    }
\$\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\$
    – Mattias
    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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.