自动插入“-"或“/"MM-YYYY 文本字段的字符? [英] Automatically inserting "-" or "/" character for MM-YYYY textfields?

查看:18
本文介绍了自动插入“-"或“/"MM-YYYY 文本字段的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发 CC 验证模板,但遗憾的是,此特定项目不支持使用标准下拉菜单/单独的文本字段来选择月/年到期字段.

I'm working on a CC Validation template at the moment, but using the standard dropdown/separate textfields for selecting the Month/Year expiration fields is unfortunately not on the cards for this particular project.

相反,我希望有一个文本字段(格式为 MM-YYYY)来捕获到期日期 - 但是,我希望这样写,这样客户就不需要输入-"或月/年条目之间的/".

Instead, I'm looking to have one textfield (in the format MM-YYYY) for capturing the expiration date - however, I'm looking to write this so that a customer is not required to enter the "-" or "/" between the Month/Year entries.

相反,在客户输入02"之后,连字符或斜线应自动出现在它后面.如果客户随后在一年中退格,则连字符/斜线也应删除,以便他们轻松编辑他们的月份数据.

Instead, after the customer types in say, "02", the hyphen or slash should automatically appear after it. If the customer then backspaces over the year, the hyphen/slash should also be removed, allowing for them to easily edit their month data.

是否有任何体面的解决方案可以实现这一点?或者是自己滚动的情况?

Are there any decent solutions available which accomplish this? Or is it a case of rolling your own?

推荐答案

请试试这个,我为日期创建的https://jsfiddle.net/dhruv1992/6fk8fb1v/

please try this, I created for date https://jsfiddle.net/dhruv1992/6fk8fb1v/

<input type="text" id="dateofbirth">

jquery

$("#dateofbirth").on('keyup',function(event){
    var key = event.keyCode || event.charCode;
    if (key == 8 || key == 46) return false;
    var strokes = $(this).val().length;
    if(strokes === 2 || strokes === 5){
        var thisVal = $(this).val();
        thisVal += '/';
        $(this).val(thisVal);
    }
});

这篇关于自动插入“-"或“/"MM-YYYY 文本字段的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆