在输入中的每第4个字符后插入破折号 [英] Insert dash after every 4th character in input
本文介绍了在输入中的每第4个字符后插入破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在输入后的每第4个字符后插入一个破折号。我有一个信用卡输入框。当用户输入并到达每个第4个字符时,jQuery将插入连字符( -
)。
I want insert a dash after every 4th character in input. I have a credit card input box. When a user is typing and reaches each 4th character, then jQuery will insert a hyphen (-
).
For示例: 1234-5678-1234-1231
更新:我正在尝试一些代码,我认为我是如此接近正确的代码,但我有一些问题。这是我的代码示例;
UPDATE: I'm trying some codes and i think i'm so close to correct code but i have some problems. Here is my code sample;
$('.creditCardText').keyup(function() {
var cardValue = $('.creditCardText').val(),
cardLength = cardValue.length;
if ( cardLength < 5 ) {
if ( cardLength % 4 == 0 ) {
console.log('4 lük geldi');
cardValue += "-";
$('.creditCardText').val(cardValue);
}
} else {
if ( cardLength % 5 == 0 ) {
console.log('5 lük geldi');
cardValue += "-";
$('.creditCardText').val(cardValue);
}
}
});
推荐答案
此jquery扩展将每四个字符插入一个空格: https://github.com/stripe/jquery.payment
This jquery extension will insert a space every four characters: https://github.com/stripe/jquery.payment
这篇关于在输入中的每第4个字符后插入破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文