天天看點

正規表達式實作座機和手機的驗證

涓? 浠g??

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>浣跨??reg_match()?芥?闆??preg_match_all()?芥?伴??璇????哄??搴ф?哄?風?????煎??????g‘</title>
</head>
<body>
<?php 
	$checktel="/^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/";		//瀹?涔?楠?璇?搴ф?哄?風????姝e??琛ㄨ揪寮?
	$checkphone="/^13(\d{9})$|^15(\d{9})$/";   								//瀹?涔?楠?璇????哄?風????姝e??琛ㄨ揪寮?
	if($_POST[check_tel]!="" and $_POST[Submit]=="??浜?){						//?ゆ?????????版????浜?		if(preg_match($checktel,$_POST[check_tel],$counts)){					//?ц?楠?璇???浣?
			echo "<script>alert('?佃???風???煎?姝g‘!');window.location.href='index.php';</script>"; 	
		}else{
			echo "<script>alert('?佃???風???煎?涓?姝g‘!');window.location.href='index.php';</script>"; 
		}
	}
	if($_POST[check_phone]!="" and $_POST[Submites]=="??浜?){					
		if(preg_match_all($checkphone,$_POST[check_phone],$counts)){
			echo "<script>alert('???哄?風???煎?姝g‘!');window.location.href='index.php';</script>"; 	
		}else{
			echo "<script>alert('???哄?風???煎?涓?姝g‘!');window.location.href='index.php';</script>"; 
		}
	}
?>
<table width="851" height="483"  align="center" cellpadding="0" cellspacing="0" background="images/bg.jpg">
  <tr>
    <td height="119" colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td width="336" height="264">&nbsp;</td>
    <td width="451" align="center" valign="top"><table width="381" height="175"  cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="3" align="center">preg_match()??preg_match_all()?芥?扮??搴???lt;/td>
  </tr><form id="form1" name="form1" method="post" action="index.php">
  <tr>
    <td width="88">搴ф?哄?風??锛?</td>
    <td width="223"><input type="text" name="check_tel" /></td>
    <td width="70"><input type="submit" name="Submit" value="??浜? /></td>
  </tr>
  <tr>
    <td width="88">???哄?風??锛?</td>
    <td width="223"><input type="text" name="check_phone" /></td>
    <td width="70"><input type="submit" name="Submites" value="??浜? /></td>
  </tr>
  
  </form>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table></td>
    <td width="64" valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td height="100">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>      

?

浜? 杩?琛????? 杈??ワ?234-54345675 杈??猴??佃???風???煎?姝g‘

php