Wednesday, June 12, 2013

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");
    }
  }
};

 

 

No comments:

Post a Comment