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

好程序員web前端分享前端 javascript 練習題正則表達式javascript

表單驗證前端

簡單的佈局:java

<div class="container" id="dv">程序員

  <label for="qq">Q Q</label><input type="text" id="qq"><span></span><br/>web

  <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/>this

  <label>姓名</label><input type="text" id="fullName"><span></span><br/></div>spa

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";

      }

    };

  }

相關文章
相關標籤/搜索