function pageLoad(sender, args) {
$(document).ready(function() {
init();
});
}
function init(){
//Text box Client ID
SetInputValidationCustom("RadGridProductSuppliers_ctl00_ctl02_ctl02_CostDueDate", /[0-9]/, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "DD/MM/YYYY", 10);
}
//Set Custom Text Field input validation
function SetInputValidationCustom(ControlID, Regex, Format, FormatValue, Maxlength, ValidatorID) {
if ($("#" + ControlID + ':enabled').length != 0) {
var $control = $("#" + ControlID);
var defaultColor = $control.css("color");
if (FormatValue != null) {
$control.focus(function() {
if ($(this).val().length == 0) {
$(this).css("color", "#999999");
$(this).val(FormatValue);
}
});
$control.blur(function() {
var returnVal = checkDateFormat($(this).val());
if (returnVal == true) {
$(this).css("color", defaultColor);
} else {
var $this = $(this);
$this.next("span").remove();
$this.after('<span style="color:red;position:absolute;display:block;background-color:white;border:1px solid gray;padding:3px;width:' + ($(this).width() - 2 )+ 'px;">Invalida date!</span>');
$(this).val("");
setTimeout(function() {
$this.next("span").animate(
{ opacity: 0 },
2000,
function() {
$this.next("span").remove();
});
}, 2000);
}
});
}
$control.keydown(function(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var position = getCaretPosition(this);
var value = $(this).val();
if (theEvent.preventDefault)
theEvent.preventDefault();
if (key == 8) {
if (position == 6 || position == 3)
$(this).val(value.substring(0, position - 2) + FormatValue.substring(position - 2, position - 1) + value.substring(position - 1));
else
$(this).val(value.substring(0, position - 1) + FormatValue.substring(position - 1, position) + value.substring(position));
setCaretPosition(this, position - 1);
position = position - 1
theEvent.returnValue = false;
} else if (key == 37) {
setCaretPosition(this, position - 1);
position = position - 1
theEvent.returnValue = false;
} else if (key == 39) {
setCaretPosition(this, position + 1);
position = position + 1
theEvent.returnValue = false;
} else {
if (key >= 96 && key <= 105) {
key -= 48
}
character = String.fromCharCode(key);
if (!Regex.test(character)) {
theEvent.returnValue = false;
} else {
if (Maxlength != null && position >= Maxlength) {
theEvent.returnValue = false;
} else {
$(this).css("color", defaultColor);
theEvent.returnValue = true;
}
if (theEvent.returnValue && (!(position == 2 || position == 5))) {
var value = $(this).val();
$(this).val(value.substring(0, position) + character + value.substring(position + 1));
setCaretPosition(this, position + 1);
position = position + 1;
}
}
}
if (position == 2 || position == 5) {
if (key == 37 || key == 8)
setCaretPosition(this, position - 1);
else
setCaretPosition(this, position + 1);
}
if ($(this).val() == FormatValue) {
$(this).css("color", "#999999");
}
});
$control.click(function() {
var position = getCaretPosition(this);
if (position == 2 || position == 5)
setCaretPosition(this, position + 1);
})
}
}
//Set text field cursor position
function setCaretPosition(ctrl, pos) {
if (ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos, pos);
}
else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
//Get text field cursor position
function getCaretPosition(ctrl) {
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0')
CaretPos = ctrl.selectionStart;
return (CaretPos);
}
function checkDateFormat(val) {
var n = val.split("/");
n[0] = parseInt(n[0]);
n[1] = parseInt(n[1])
n[2] = parseInt(n[2])
if (n[0] < 1 || n[0] > 31) {
return "There is no date as " + n[0]+"!";
}
if (n[1] < 1 || n[1] > 12) {
return "There is no month as " + n[1] + "!";
}
if (n[2] < 1) {
return "There is no year as " + n[2] + "!";
}
if (n[0] == 31) {
var month = [2, 4, 6, 9, 11];
for (var m in month) {
if (n[1] === month[m]) {
return "There is no " + n[0] + " in " + (new Date(n[1] + "/01/2012") + "").substring(7, 4) + " " + n[2] + "!";
}
}
}
if (n[1] == 2) {
if (n[2] % 4 == 0) {
if (n[0] < 1 || n[0] > 29) {
return "There is no " + n[0] + " in " + (new Date(n[1] + "/01/2012") + "").substring(7, 4) + " " + n[2] + "!";
}
} else {
if (n[0] < 1 || n[0] > 28) {
return "There is no " + n[0] + " in " + (new Date(n[1] + "/01/2012") + "").substring(7, 4) + " " + n[2] + "!";
}
}
}
return true;
}