好程序員web前端分享前端javascript練習題

好程序員web前端分享前端 javascript 練習題,正則表達式
表單驗證
簡單的佈局:
<div class="container" id="dv">
<label for="qq">Q Q</label><input type="text" id="qq"><span></span><br/>
<label>手機</label><input type="text" id="phone"><span></span><br/>
<label>郵箱</label><input type="text" id="e-mail"><span></span><br/>
<label>座機</label><input type="text" id="telephone"><span></span><br/>
<label>姓名</label><input type="text" id="fullName"><span></span><br/></div>
js代碼:
checkInput(my$("qq"),/^\d{5,11}$/); //qq的
checkInput(my$("phone"),/^\d{11}$/); //手機
checkInput(my$("e-mail"),/^[0-9a-zA-Z.-]+[@][0-9a-zA-Z.-]+([.][a-zA-Z]+){1,2}$/); //郵箱
checkInput(my$("telephone"),/^\d{3,4}[-]\d{7,8}$/); //座機號碼
checkInput(my$("fullName"),/^[\u4e00-\u9fa5]{2,6}$/); //中文名字//通過正則表達式驗證當前的文本框是否匹配並顯示結果
function checkInput(input,reg) {
//文本框註冊失去焦點的事件
input.onblur=function () {
if(reg.test(this.value)){
this.nextElementSibling.innerText="正確了";
this.nextElementSibling.style.color="green";
}else{
this.nextElementSibling.innerText="讓你得瑟,錯了吧";
this.nextElementSibling.style.color="red";
}
};
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章