我正在寻找一种解决方案,为用户提供一个友好的字段,该字段可以根据他们键入的内容来格式化日期。我注意到很多应用程序都这样设置。因此,例如,当用户键入前两个数字时,它将插入/。我愿意接受任何建议。谢谢。这只是一个标准输入类型:
<input type="text" name="input" placeholder="MM/DD/YYYY" required
pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))"
title="Enter a date in this format MM/DD/YYYY"/>
答案 0 :(得分:0)
这对我来说似乎很有效:How to auto format textbox inputs
$("input[name='birthdate']:first").keyup(function(e){
var key=String.fromCharCode(e.keyCode);
if(!(key>=0&&key<=9))$(this).val($(this).val().substr(0,$(this).val().length-1));
var value=$(this).val();
if(value.length==2||value.length==5)$(this).val($(this).val()+'/');
});
答案 1 :(得分:0)
$(function() {
$("#birthdate").datepicker();
});
$("input[name='birthdate']:first").keyup(function(e) {
var key = String.fromCharCode(e.keyCode);
if (!(key >= 0 && key <= 9)) $(this).val($(this).val().substr(0, $(this).val().length - 1));
var value = $(this).val();
if (value.length == 2 || value.length == 5) $(this).val($(this).val() + '/');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<tr>
<td><label>Birthdate</label>
<input type="text" placeholder="mm/dd/yyyy" name="birthdate" id="birthdate" maxlength="10" />
</td>
</tr>