最近都在玩
open source 的程式,方便加速自己開發 PHP 的專案,在申請帳號密碼部份就可以利用
Validate 套件來驗證,以及 email 填寫正確性,可以檢查 MX 或者是 A record 紀錄,還蠻方便的,也可以檢查
multiple 欄位,設計的相當不錯,也有金融相關套件可以驗證
CreditCard,金融套件名稱是
Validate_Finance 裡面的
Validate_Finance_CreditCard 部份,線上也有很多相關說明,可以參考
Validate 線上手冊,目前已經到 0.8.2 (beta),如果使用 Release 版本,請選用 0.8.1,軟體可以在此
下載,0.8.2 是在 2009-01-31 Release 出來的,還不錯用,最主要的功能如下
Package to validate various datas. It includes :
– numbers (min/max, decimal or not)
– email (syntax, domain check, rfc822)
– string (predifined type alpha upper and/or lowercase, numeric,…)
– date (min, max, rfc822 compliant)
– uri (RFC2396)
– possibility valid multiple data with a single method call (::multiple)
- 驗證各種不同的日期函式
- 驗證數字(最小/最大,是否是10進位)
- email 驗證(正規語法驗證,check domain name 是否存在,rfc822 驗證)
- 字串驗證(正規語法驗證,是否包含數字英文字母,可輸入最長或最短)
- url 驗證(遵從 RFC2396 規定)
- 多重欄位(multiple data)驗證(可以同時驗證上述功能)
來介紹
Validate 套件用法:
1.
Validate::date 這部份我比較不推薦用這裡的方法,可以用 jQuery 的套件:
[jQuery筆記] 好用的日期函式 datepicker,那底下是這個函式用法:必須多安裝
Pear Date 套件
/**
* 驗證此套件必須在加裝 Date_Calc class
*
* @param string $date 需要驗證的值
* 'format' 可以自行制定 (%d-%m-%Y)
*
* 'min' array($day, $month, $year)
* 'max' array($day, $month, $year)
* 回傳值 true or false
*/
$values = "2009-01-10";
$opts = array(
'format'=> "%Y-%d-%m",
'min' => array('02','10','2009'),
'max' => array('05','10','2009')
);
$result = Validate::date($values, $opts);
if($result)
{
echo $result;
}
else
{
echo "error";
}
2.
Validate::email
/**
*
* @param string $email 您要驗證的 email
* @param mixed
* check_domain 檢查 domain 是否有 A 或者 MX Record
* use_rfc822 檢查 domain 是否符合 rfc822 規範
* 回傳值 true or false
*
*/
$values = "appleboy@XXXXX";
$options = array(
'check_domain' => 'true',
'fullTLDValidation' => 'true',
'use_rfc822' => 'true',
'VALIDATE_GTLD_EMAILS' => 'false',
'VALIDATE_CCTLD_EMAILS' => 'false',
'VALIDATE_ITLD_EMAILS' => 'false',
);
$result = Validate::email($values, $options);
if($result)
{
echo $result;
}
else
{
echo "error
";
}
3.
Validate::number:
/**
*
* @param string $number 您要驗證的數字
* param array $options array 選單
* decimal 可否在數字中間出現非數字符號如:., 代表可以使用 .,
* dec_prec 有多少 decimal 可以出現在數字中
* min 最小值
* max 最大值
* 回傳值 true or false
*
*/
$values = "12300,45";
$options = array(
'decimal' => ',',
'dec_prec' => '2',
'min' => '1000',
);
$result = Validate::number($values, $options);
if($result)
{
echo $result;
}
else
{
echo "error
";
}
4.
Validate::String
/**
*
* @param string $string 您要驗證的字串
* param array $options array 選單
* 'format' 字串的格式
* Ex:VALIDATE_NUM(數字 0-9) . VALIDATE_ALPHA_LOWER(a-z)
* VALIDATE_ALPHA_UPPER (A-Z) VALIDATE_ALPHA(a-zA-Z)
* dec_prec 有多少 decimal 可以出現在數字中
* 'min_length' 字串最小長度
* 'max_length' 字串最大長度
* 回傳值 true or false
*
*/
$values = "appleboy";
$options = array(
'format' => VALIDATE_NUM,
'min_length' => '2',
'max_length' => '1000',
);
$result = Validate::number($values, $options);
if($result)
{
echo $result;
}
else
{
echo "error
";
}
5.
Validate::Uri
/**
* Validate an URI (RFC2396)
* EX:
* $options = array('allowed_schemes' => array('http', 'https', 'ftp'))
* var_dump(Validate::uri('http://www.example.org', $options));
* 注意事項:
* 1. 在 RFC2396 裡面定義了"-" 這個符號可以使用在 URI 裡面
* e.g. http://example.co-m 這是可以驗證通過的
* 但是在any known TLD裡面是不被准許的
* 2.
* @param string $url 您要驗證的網址列
* @param array $options Options used by the validation method.
* 'domain_check' 檢查 domain 是否存在 DNS 裡
* 'allowed_schemes' 陣列 for list protocols ex: https, http
* 'strict' 預設值: ';/?:@$,' 空值: accept all rfc2396 foreseen chars
* 回傳值 true or false
*
*/
$options = array(
'check_domain'=>true,
'allowed_schemes' => array('http','https'),
'strict' => ';/?:@$,'
);
var_dump(Validate::uri('http://blog.wu-boy.com/', $options));
6.
Validate::multiple 結合上述的語法,整合成一個函式
$values = array(
'amount'=> '13234,344343',
'name' => 'foo@example.com',
'mail' => 'foo@example.com',
'mystring' => 'ABCDEabcde'
);
$opts = array(
'amount'=> array('type'=>'number','decimal'=>',.','dec_prec'=>null,'min'=>1,'max'=>32000),
'name' => array('type'=>'email','check_domain'=>false),
'mail' => array('type'=>'email'),
'mystring' => array('type'=>'string',array('format'=>VALIDATE_ALPHA, 'min_length'=>3))
);
$result = Validate::multiple($values, $opts);
print_r($result);
Related