javascript常用Regex代碼

來源:互聯網
上載者:User

網頁特效常用Regex代碼

html

<p>
<label for="mobile_phone">手 機</label>
<input type="text" id="mobile_phone", name="mobile_phone" value="15107105287" />
<input type="button" value="validate" onclick="ismobile()" />
</p>
<p>
<label for="phone">座 機</label>
<input type="text" id="phone" name="phone" value="027-87767676" />
<input type="button" value="validate" onclick="isphone()" />
</p>
<p>
<label for="email">郵 箱</label>
<input type="" id="email" name="email" value="zhangchen2397@126.com" />
<input type="button" value="validate" onclick="isemail()" />
</p>

js

function ismobile() {
var mobile = document.getelementbyid("mobile_phone");
var num = mobile.value;
var reg = /^(13[0-9]|186|188|150|151|158|159|147)d{8}$/;
if(num == "") {
alert("請輸入完整的手機號");
mobile.focus();
return false;
} else if (reg.test(num)) {
alert("輸入的手機號格式正確");
} else {
alert("請輸入正確的11位手機號碼");
mobile.focus();
return false;
}
}
function isemail() {
var email = document.getelementbyid("email");
var email_value = email.value;
if(email_value == "") {
alert("請輸入完整的郵箱");
email.focus();
return false;
} else {
var reg = /^[a-za-z0-9](w)+@(w)+(.)+(com|com.cn|net|cn|net.cn|org|biz|info|gov|gov.cn|edu|edu.cn)$/;
if(reg.test(email_value)) {
alert("輸入的郵箱格式正確");
} else {
alert("請輸入正確的郵箱格式");
email.focus();
return false;
}
}
}
function isphone() {
var phone = document.getelementbyid("phone");
var phone_value = phone.value;
if(phone_value == "") {
alert("請輸入完整的有線電話號碼");
phone.focus();
return false;
} else {
var reg = /^[(]?0d{2,3}[)]?s*[-]?s*d{7,8}$/; //010-87989898 01098989898 (0712)8989898 010 - 23343434 這些格式的有線電話號碼都滿足
if(reg.test(phone_value)) {
alert("輸入的有線電話號碼正確");
} else {
alert("輸入的有線電話號碼格式錯誤");
phone.focus();
return false;
}
}
}

常用的正則

•. 尋找單個字元,除了換行和行結束符;
•w 匹配字母、漢字、數字、底線等符號;
•s 匹配空白符(包含空格、定位字元等);
•d 匹配數字;
•b 匹配位於單詞的開頭或結尾的匹配;
常用的量詞有:
•^n 匹配任何開頭為 n 的字串;
•n$ 匹配任何結尾為 n 的字串;
•n+ 匹配任何包含至少一個 n 的字串;
•n* 匹配任何包含零個或多個 n 的字串;
•n? 匹配任何包含零個或一個 n 的字串;
•n{x} 匹配包含 x 個 n 的序列的字串;
•n{x, y} 匹配包含 x 或 y 個 n 的序列的字串;
簡單舉例,主要用於驗證手機號碼、電話號碼及郵箱:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.