日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

2013/02/06用MM/MM/MMMM替換

 zww_blog 2013-11-07

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

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約