達令說
2023-09-28 09:37:49
我的html是:<input class="UserInfo" type="text" placeholder="phone Format" id="Phone_num">這是我的js:function checkPhoneFormat(){const phone = document.getElementById("Phone_num").value;const phoneFormatRex = /^\+?[0-9(),.-]+$/;var match = phoneFormatRex.exec(phone);if (match) { document.getElementById("Phone_num").value = phone;}else { document.getElementById("Phone_num").value = "";}}我想要的是在用戶單擊輸入字段外部后檢查手機的格式?
3 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
document.getElementById("Phone_num").value
和
document.getElementById("phone_num").value
有一個拼寫錯誤,屬性值始終區分大小寫。id 值應該是Phone_num
或者phone_num

SMILET
TA貢獻1796條經驗 獲得超4個贊
這是你想要的?
var input = document.getElementById("Phone_num");
input.addEventListener("blur", function(){
const phone = document.getElementById("Phone_num").value;
const phoneFormatRex = /^\+?[0-9(),.-]+$/;
var match = phoneFormatRex.exec(phone);
if (match) {
document.getElementById("Phone_num").value = phone;
}
else {
document.getElementById("Phone_num").value = "";
}
})
<input class="UserInfo" type="text" placeholder="phone Format" id="Phone_num">
添加回答
舉報
0/150
提交
取消