Validate Time w prototype ptrn

by Terrance Smith

HTML

<span id="err" />
<input type="text" id="time" />
<input type="button" id="validate" value="Validate" />

JavaScript

var ClientDataNewJS = function () { };
ClientDataNewJS.prototype = function () {
     //for asp.net custom validation
     var _that = this,
         _isValidTime = function (src, args) {
             var input = args.Value;
             var result = validateTime(input);

             args.IsValid = result;
             return;
         },
         _validateTime = function (input) {
             return Date.parseExact(input, [
                 "hh:mm:ss tt",
                 "h:mm:ss tt",
                 "h:m:s tt",
                 "h:mt",
                 "h:m t",
                 "ht", "h t"]) !== null || Date.parseExact(input, [
                 "h:mtt",
                 "h:m tt",
                 "htt", "h tt"]) !== null;
         },
         _validateTimeElementIfPopulated = function (elementId, errId, errMsg, errHtml) {
             var testElement = $("#" + elementId),
                 validationSpan = $("#" + errId),
                 isPopulated = (testElement.val().toString().length > 0),
                 retval = true;

             if (isPopulated) {
                 if (!validateTime(testElement.val())) {
                     alert(errMsg);
                     validationSpan.html(errHtml);
                     testElement.focus();
                     retval = false;
                 }
             }
             return retval;
         };

     return {
         isValidTime: _isValidTime,
         validateTime: _validateTime,
         validateTimeElementIfPopulated: _validateTimeElementIfPopulated
     };
 };

 
$(document).ready(function () {
    instance = new ClientDataNewJS();
    $("#validate").click(function () {
         alert(instance.isValidTime("7:00 pm"));
         //instance.validateTimeElementIfPopulated("time", "err", "fail", "<strong>fail</strong>");
     });
 });