Create Validation for JQuery UI DatePicker exapmle

BresleveloperNS :http://bresleveloper.blogspot.co.il/2013/06/my-extenxtion-javascript-namespace.html
 
BresleveloperNS = BresleveloperNS || {};

BresleveloperNS.ValidateDatePicker = function (dateSelector, errorMsg) {
  $(dateSelector).blur(function () {
    BresleveloperNS.ValidateDatePicker_ValidateHandler(this, errorMsg);
  });
}

BresleveloperNS.ValidateDatePicker_All =
function (errorMsg) {
  $(".hasDatepicker").blur(function () {
    BresleveloperNS.ValidateDatePicker_ValidateHandler(this, errorMsg);
  });
}

BresleveloperNS.ValidateDatePicker_ValidateHandler =
function (dateSelector, errorMsg) {
  var txtDate = dateSelector.val();
  var isTxtDateValid = true;
  var dateFormat = $(dateSelector).datepicker("option", "dateFormat");
  try {
    txtDate = $.datepicker.parseDate(dateFormat, txtDate);
  }
  catch (ex) {
    if (ex == "Invalid date") {
      isTxtDateValid = false;
    }     else {
      throw ex;
    }
  }

  
if (txtDate !== null) {
    var maxDate = dateSelector.datepicker("option", "maxDate");
    var minDate = dateSelector.datepicker("option", "minDate");
    if (maxDate != null) {
      maxDate.setHours(0, 0, 0, 0);       if (txtDate > maxDate) {
        isTxtDateValid = false;
      }
    }
    if (minDate != null) {
      minDate.setHours(0, 0, 0, 0)       if (txtDate < minDate) {
        isTxtDateValid = false;
      }
    }
  }

  
if (!errorMsg) {
    errorMsg = "Invalid Date";
  }
  

  if (isTxtDateValid) {
    if (dateSelector.next("span.ValidateDatePicker_ErrMsg").length > 0) {
      dateSelector.next("span.ValidateDatePicker_ErrMsg").hide();
    }
  }
  else {
    dateSelector.val("");
    if (dateSelector.next("span.ValidateDatePicker_ErrMsg").length > 0) {
      dateSelector.next("span.ValidateDatePicker_ErrMsg").show();
    }     else {
      dateSelector.after("<span class='ValidateDatePicker_ErrMsg'>" + errorMsg + "</span>");
      dateSelector.next("span.ValidateDatePicker_ErrMsg").css("color", "red").css("font-weight", "bold");
    }
  }
};

 

 

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem