Study Area Facebook粉絲團http://www.facebook.com/sataiwan
0 會員 與 1 訪客 正在閱讀本文。
Package to validate various datas. It includes :- numbers (min/max, decimal or not)- email (syntax, domain check, [url=http://www.w3.org/Protocols/rfc822/]rfc822[/url])- string (predifined type alpha upper and/or lowercase, numeric,…)- date (min, max, rfc822 compliant)- uri ([url=http://www.w3.org/2002/11/dbooth-names/rfc2396-numbered_clean.htm]RFC2396[/url])- possibility valid multiple data with a single method call (::multiple)
/*** 驗證此套件必須在加裝 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";}
/** * * @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";}
/** * * @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<br />";}
/** * * @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<br />";}
/** * 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. [url]http://example.co[/url]-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));
$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);