javascript 練習題,正則表達式
表單驗證
簡單的佈局:
Q Q
手機
郵箱
座機
姓名
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";
}
};
}