萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 服務器與客戶端的郵箱地址驗證函數

服務器與客戶端的郵箱地址驗證函數

   郵箱地址驗證有很多方法。在浏覽器端,js郵箱驗證可以通過正則表達式檢測。

 代碼如下  

function isEmail(email) {
    return /^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])))$/i.test(email);
}

function validateEmail(elementValue) 
{    
var emailPattern = /^[a-zA-Z0-9._]+[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]{2,4}$/;    
return emailPattern.test(elementValue);
}

function checkEmail(mail) {    
return /^[w-]+(.[w-]+)*@([a-z0-9-]+(.[a-z0-9-]+)*?.[a-z]{2,6}|(d{1,3}.){3}d{1,3})(:d{4})?$/.test(mail);
}

  調用很簡單:

 代碼如下  


if (isEmail('[email protected]')){ console.log(' www.111cn.net This is email is valid'); }View Code 

  如果是進行服務器端驗證。像php,則最簡單的莫過於:

 代碼如下  

$a='/([w._]{2,10})@(w{1,}).([a-z]{2,4})/';
$b='[email protected]';

if(preg_match($a,$b)){
 echo "電子郵件合法";
}else{
 echo "電子郵件不合法啊";
}

  你要判斷一整段字符串是否都匹配就要用斷言。如^ $

  /^([w._]{2,10})@(w{1,}).([a-z]{2,4})$/

  注意頭和尾加上了^ $

copyright © 萬盛學電腦網 all rights reserved