简体   繁体   English

当使用日期选择器,按键上的逗号替换为点时,如何在输入字段中输入日期

[英]How to in input field date when use datepicker, when pres comma on presskey replaced with dot

I use datepicker and input form for input date. 我使用datepicker和输入表单输入日期。 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). 如何允许我在格式输入字段日期(type=text)按逗号,同时在输入字段中显示点(用点替换逗号)。 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 的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>

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 . 该属性的最大长度不是大小,因此请在输入字段中设置maxlength = 10。我想您还需要使用日期格式来验证输入字符串。 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 有关格式,请参见此处:正则表达式以验证日期格式dd / mm / yyyy

Datepicker is now look like this. Datepicker现在看起来像这样。 It is important to add on close, becaouse is need when You pick date from calendar. 重要的是要添加收盘价,因为从日历中选择日期时需要这样做。

$(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()
   { 
   var data;
   data=$('#dokumentdatum').val();
   var str=data.replace(/\,/g,".");
   $('#dokumentdatum').val(str.replace(/\.\.+/g, '.'));
   this.focus(); 
   }                       
 } );

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM