问题描述:

How is change normal number formatting to date formatting in here (http://jsfiddle.net/prL83/)?

typed date from right to left dir="rtl" in this format, YYYY/MM/DD.

  1. first typed the day(DD)
  2. secend typed the month(MM)
  3. thrid typed the year(YYYY)

    $('.find_input').delegate("input.numeric:text", 'keyup', function () {

    $val = $(this).val().match(/[0-9]/g).reverse().join("").match(/[0-9]{1,3}/g).join(",").match(/./g).reverse().join("");

    $(this).val($val)

    });

网友答案:

Will this work?

$dateSet = 0;
$monthSet = 0;
$yearSet = 0;

$('.find_input').delegate("input.numeric:text", 'keyup', function () {
    $val = $(this).val().replace(/[^\d]+/g, "").match(/\d{1,12}$/);
    if($val == null) {
        return;
    } else {
        $val = $val.join("");
    }

    if($(this).val().match(/\d{4,}$/) && $val.length%2 == 0) {
        $val = $val.match(/\d{2}/g);
        if($yearSet < $monthSet) {
            if($val.length == 4) {
                $(this).val($val.join("").replace(/(\d{2})(\d{2})(\d{4})$/,'$3/$1/$2'));
                $yearSet++;
            } else if($val.length == 6){

                $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{4})$/,'$4/$2/$3'));
                $yearSet++;
            }
        } else {
            if($monthSet < $dateSet) {
                $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$4/$3'));
                $monthSet++;
            } else {

                if($val.length == 2) {
                       $(this).val($val.reverse().join("/"));
                    $dateSet++;
                    $monthSet++;
                } else {
                       $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$2/$4'));
                    $dateSet++;
                }
            }
        }
    }
});
相关阅读:
Top