dsv768456 2015-11-13 10: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 11: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条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像