如何设置我的输入类型用括号和破折号分隔数字?
Number : <input type="text" name="quantity" id="quantity" />
<span id="errmsg"></span>
$("#quantity").keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
答案 0 :(得分:0)
快乐编码:)
// Used to format phone number and add placeholder and max-length
function phoneFormatter() {
$(' input[type="text"]').attr({ placeholder : '(___) ___-____' });
$('input[tabindex="111"]').on('input', function() {
var number = $(this).val().replace(/[^\d]/g, '')
if (number.length == 7) {
number = number.replace(/(\d{3})(\d{4})/, "$1-$2");
} else if (number.length == 10) {
number = number.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
}
$(this).val(number)
$('input[type="text"]').attr({ maxLength : 10 });
});
};
$(phoneFormatter);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="input_111[]" value="" tabindex="111">