php 表單驗證代碼_PHP教程

來源:互聯網
上載者:User
提供一款檢查php表單檢證與檢查效果代碼。

function validate($value,$onoff=0,$type=3,$len=100,$msg='填寫錯誤') {


if(trim($value) != ""){ //如果有內容

if(!is_int($len))
exit("");

preg_match_all("/[xa0-xff]?./",$value,$arr); //中英文混排算字串長度
$qswhLen_v=count($arr[0]);
if($qswhLen_v> $len)
{
exit("");

}

if ($type==0){ //類型為0的話 檢查資料是否為數字
if(!eregi('^[0-9]+$', $value)){
exit("");
}
}
else if ($type==1){ //類型為1的話 檢查資料是否為字母
if(!eregi('^[a-zA-Z]+$',$value)){
exit("");
}
}
else if ($type==2){ //類型為2的話 檢查資料是否為中文
if(!eregi("^[".chr(0xa1)."-".chr(0xff)."]+$",$value)){
exit("");
}
}
else if ($type==3){ //類型為3的話 檢查資料是否為字母和數字
if(!eregi('^[0-9a-zA-Z]+$',$value)){
exit("");
}
}
else if ($type==4){ //類型為4的話 email檢查
if(!eregi("^[_.0-9a-zA-Z-]+@([0-9a-zA-Z_-]+.)+[a-zA-Z]$", $value)){
exit("");
}
}
else if ($type==5){ //類型為5的話 日期格式檢查
if(!eregi('^[1-2]{1}[0-9]{3}-[0-9]{2}-[0-9]{2}',$value)){
exit("");
}
}
else if ($type==6){ //類型為6的話 社會安全號碼碼格式檢查
if(!preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$value)){
exit("");
}
}
else if ($type==7){ //類型為7的話 電話號碼格式檢查
if(!preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,7}$/",$value)){
exit("");
}
}
else if ($type==8){ //類型為8的話 郵編格式檢查
if(!preg_match("/^[1-9]d{5}$/",$value)){
exit("");
}
}
else if ($type==9){ //類型為9的話 url地址格式檢查
if(!preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$value)){
exit("");
}
}
else if ($type==10){ //類型為10的話 無限制

}else{
echo "";
exit;
}

}else{
if($onoff!=0){
echo "";
exit;
}
}

}


http://www.bkjia.com/PHPjc/445048.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445048.htmlTechArticle提供一款檢查php表單檢證與檢查效果代碼。 function validate($value,$onoff=0,$type=3,$len=100,$msg='填寫錯誤') { if(trim($value) != ){ //如果有內容 if(!is_in...

  • 聯繫我們

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