PHP 验证字符串、数字、日期格式是编程过程中经常需要使用的功能,下面是示例代码。
<?php
function is_valid_username($username){
// accepted username length between 5 and 20
if (preg_match(‘/^\w{5,20}$/’, $username))
return true;
else
return false;
}
function is_valid_password($pwd){
// accepted password length between 5 and 20, start with character.
if (preg_match(“/^[a-zA-Z][0-9a-zA-Z_!$@#^&]{5,20}$/”, $pwd))
return true;
else
return false;
}
function is_valid_date($date){
//============ date format dd-mm-yyyy
if(preg_match(“/^(\d{2})-(\d{2})-(\d{4})$/”, $date, $sdate))
if(checkdate($sdate[2], $sdate[1], $sdate[3]))
return true;
}?>