溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用javascript正則表達式

發(fā)布時間:2021-11-06 10:31:21 來源:億速云 閱讀:109 作者:iii 欄目:web開發(fā)

本篇內容介紹了“怎么使用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";

      }

    };

  }

“怎么使用javascript正則表達式”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI