$('#phone-number', '#example-form')
.keydown(function (e) {
var key = e.charCode || e.keyCode || 0;
$phone = $(this);
// Adding blankspace in 9th, 13th and 17th position.
//Change it according to your need
if (key !== 8 && key !== 9) {
if ($phone.val().length === 8) {
$phone.val($phone.val() + ' ');
}
if ($phone.val().length === 12) {
$phone.val($phone.val() + ' ');
}
if ($phone.val().length === 16) {
$phone.val($phone.val() + ' ');
}
}
// Allow numeric number and tab, backspace, delete keys only.
//Change it according to your need
return (key == 8 ||
key == 9 ||
key == 46 ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
.bind('focus click', function () {
$phone = $(this);
if ($phone.val().length === 0) {
$phone.val('+'); // as your phone number start with '+'
}
else {
var val = $phone.val();
$phone.val('').val(val); // Ensure cursor remains at the end
}
})
.blur(function () {
$phone = $(this);
if ($phone.val() === '+') {
$phone.val(''); // if somebody type '+' it will be vanish
}
});
2 回答
这不可能 .
您只能在输入的末尾添加可选参数 .
他们在Github有一个开放式拉请求 .
你可以查一下here .
以下代码正常,您可以尝试一下 . 我已经为您创建了solution in JSfidller . 如果你想要检查它 . HTML:
Script: