dsv768456 2015-11-13 02:36
浏览 46
已采纳

当使用datepicker时,如果在输入字段日期,当按下逗号键时用逗号键替换为点

I use datepicker and input form for input date. Sometimes need, becouse of better speed only input numbers from keyboard without datapicker. How I can allowed on format input field date (type=text), press comma and in same moment show dot in input field (replace comma with dot). This is becouse keyboard num pad just has a comma and not a dot. Now I can not pres comma. Allowed now is only dot.

script

<script>
$(function() {
$( "#dokumentdatum" ).datepicker( { changeMonth: true , changeYear:   
true, yearRange:"-100:+100", dateFormat: "dd.mm.yy",
dayNamesMin: ["Su", "Ne", "Po", "Ut", "Sr", "Če", "Pe"],
monthNamesShort: [ "Sij", "Vel", "Ožu", "Tra", "Svi", "Lip", "Srp", "Kol", "Ruj", "Lis", "Stu", "Pro" ],
firstDay: 2, showOn: "button",  buttonText: 'Odaberi datum', buttonImageOnly: true, buttonImage: 'http://jqueryui.com/resources/demos/datepicker/images/calendar.gif' , 
onClose: function()
   { this.focus(); }                       
 } );

 $(".ui-datepicker-trigger").mouseover(function() {
    $(this).css('cursor', 'pointer');       
   });                           
 });  
</script>

php

<p><label class="field4" for "Dokumentdatum">Datum dokumenta : </label> <input type="text" id="dokumentdatum" onblur="prepisidatumdvo(this)"  name="dokumentdatum" value="'.$dokumentdatum.'" placeholder="dd.mm.yyyy" size="10" class="textbox-xx" ></p>
  • 写回答

2条回答 默认 最新

  • douqian2524 2015-11-13 03:56
    关注

    the property in maxlength not size so set maxlength=10 in your input field.I guess you also need to validate the input string with the date format . you can try this:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        var lastValue=""
        $(function(){
            $('#dokumentdatum').on('input', function(e){
                        var data;
                data=$('#dokumentdatum').val();
                var count= data.length, ml= 10,remaining= ml - count;
                    if(remaining <= 0){
                    return;
                    }
                   var str=data.replace(/\,/g,".");
                $('#dokumentdatum').val(str.replace(/\.\.+/g, '.'));
            }); 
        });
    });
    
     function checkFormat() {
         var reg = /^(?:(?:31(\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/g;
         var txt = $('#dokumentdatum').val();
         if (reg.test(txt)) {
             document.getElementById("mainDiv").innerHTML = "<h1 style='color:green'>Correct</h1>";
         } else {
             document.getElementById("mainDiv").innerHTML = "<h1 style='color:red'>Wrong</h1>";
         }
     }
    </script>
    <p><label class="field4" for "Dokumentdatum">Datum dokumenta : </label> <input type="text" id="dokumentdatum" onblur="checkFormat()"  name="dokumentdatum" value="" placeholder="dd.mm.yyyy"  class="textbox-xx" maxlength="10"></p>
    
    <div id="mainDiv"></div>

    see here for formats:Regex to validate date format dd/mm/yyyy

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部