在input框輸入手機號碼時,自動加入空格按照3,4,4位顯示,如:
實現方法如下:
<label>手機號碼</label>
<input type="text" id="category">
<script>
$("#category").keyup(function(){
var len=$("#category").val().length;
var reg = new RegExp("\\s","g");
var mobile_ = "";
var mobile=$("#category").val();
// 去掉空格
mobile = mobile.replace(reg,"");
for(var i = 0;i < len;i++){
if(i == 2||i == 6)
{
mobile_ = mobile_ + mobile.charAt(i) + " ";
}
else
{
mobile_ = mobile_ + mobile.charAt(i);
}
}
$("#category").attr("value",mobile_);
});
</script>
手機號碼驗證另寫。