开发者

Problem in make a date input mask

开发者 https://www.devze.com 2023-04-03 01:28 出处:网络
How do I m开发者_运维技巧ake a date input masked (hidden) without using a plugin, as followed:

How do I m开发者_运维技巧ake a date input masked (hidden) without using a plugin, as followed:

Formating: YYYY/MM/DD = showing in the input as => ____/__/__

This is my code that doesn't work:

$('.date_input').delegate("input.date:text", 'keyup', function () {
    //var dateMMDDYYYRegex = '^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$';
    $val = $(this).val().match(/[0-9]/g).reverse().join("").match(/[0-9]{3,3}/g).join("/").match(/./g).reverse().join("");
    $(this).val($val)
});

Example of the code


$('.date_input').delegate("input.date:text", 'keypress', function (e) {
    var char = getChar(e || window.event);
    if (char != "/")
    {
        this.value += "_";
        return false;
    }
});

function getChar(event) {
  if (event.which == null) {
    return String.fromCharCode(event.keyCode) // IE
  } else if (event.which!=0 && event.charCode!=0) {
    return String.fromCharCode(event.which)   // the rest
  } else {
    return null // special key
  }
}


//You can replace getChar with the jQuery equivalent.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号