[英]Jquery: change input text value on focusout
You can use regex below to format your phone phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4");
您可以使用下面的正则表达式来格式化您的手机
phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4");
and set maxlength
=11并设置
maxlength
=11
$('#phone').focusout(function() {
function phoneFormat() {
phone = phone.replace(/[^0-9]/g, '');
phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4");
return phone;
}
var phone = $(this).val();
phone = phoneFormat(phone);
$(this).val(phone);
});
$('#phone').focusout(function() { function phoneFormat() { phone = phone.replace(/[^0-9]/g, ''); phone = phone.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, "$1-$2-$3-$4"); return phone; } var phone = $(this).val(); phone = phoneFormat(phone); $(this).val(phone); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="text" value="" id="phone" maxlength="11" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.