Regex的Wed驗證應用(40)

來源:互聯網
上載者:User
電子郵件地址的校正

php/* 校正郵件地址*/function checkMail($email) {//使用者名稱,由“\w”格式字元、“-”或“.”組成$email_name= "\w|(\w[-.\w]*\w)";//網域名稱中的第一段,規則和使用者名稱類似,不包括點號“.”$code_at= "@";$per_domain= "\w|(\w[-\w]*\w)";//網域名稱中間的部分,至多兩段$mid_domain= "(\." .$per_domain. "){0,2}";//網域名稱的最後一段,只能為“.com”、“.org”或“.net”$end_domain= "(\.(com|net|org))";$rs= preg_match("/^{$email_name}@{$per_domain}{$mid_domain}{$end_domain}$/",$email);return (bool)$rs;}//測試,下面均返回成功var_dump( checkMail("root@localhost") );var_dump( checkMail("Frank.Roulan@esun.edu.org") );var_dump( checkMail("Tom.024-1234@x-power_1980.mail-address.com") );?>

URL地址的校正

php/* 校正URL地址*/function checkDomain($domain){return ereg("^(http|ftp)s? ://(www\.)?.+(com|net|org)$", $domain);}$rs= checkDomain("www.taodoor.com");//返回假$rs= checkDomain("http://www.taodoor.com");//返回真?>

電話號碼

php/* 校正電話號碼*/function checkTelno($tel){//去掉多餘的分隔字元$tel= ereg_replace("[\(\)\. -]", "", $tel);//僅包含數字,至少應為一個6位的電話號(即沒有區號)if(ereg("^\d+$", $tel)){returntrue;}else{returnfalse;}}$rs= checkTelno("(086)-0411-12345678");//返回真?>

郵遞區號的校正

php/* 校正郵遞區號*/function checkZipcode($code){//去掉多餘的分隔字元$code = preg_replace("/[\. -]/", "", $code);//包含一個6位的郵遞區號if(preg_match("/^\d{6}$/", $code)){returntrue;}else{returnfalse;}}$rs= checkZipCode("123456");//返回真?>

以上就介紹了Regex的Wed驗證應用(40),包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.