關於input手機號的驗證
一、手機號的判斷方法:
function isPhoneTel(n){ var reg = /^1[3|4|5|8]\d{9}$/; if(!!(reg.test(n))){ return true; } else{ return false; } }
二、input綁定事件
phone: function(){ var _this = this; // 只可以輸入數字、刪除等鍵 _this.kphone.onkeydown = function(e){ e = e || window.event; var code = e.keyCode; if(!((code >= 48 && code <= 57) || (code >= 96 && code <= 105) || code === 8 || code === 13 || code===46 || code === 37 || code === 39)){ return false; } } // 當是11位手機號時觸發回調函數 _this.kphone.onkeyup = function(e){ e = e || window.event; } // 當失去焦點時判斷是不是正確手機號 _this.kphone.onblur = function(){if(_this.isPhoneTel(_this.kphone.value)){ _this.ktip.innerHTML = "您的手機號:"+_this.kphone.value; } else{ _this.ktip.innerHTML = "請輸入正確的手機號碼!"; } } }
例子: