The code is as follows |
Copy Code |
Class validation{ function Check_email ($value) {//check a email address return (BOOL) Preg_match ('/^[a-z0-9._%+-]+@: [a-z0-9-]+.) +[a-z]{2,4}$/i ', $value); }
function Check_ip ($value) {//check A IP address return (BOOL) Preg_match (/^:(? : 25[0-5]|2[0-4][0-9]| [01]? [0-9] [0-9]?). {4}$/", $value."); }
function Check_date ($date, $format = "Dd/mm/yy") {//check a date if (!preg_match ()/([0-9]+) ([./-]) ([0-9]+) (\2) ([0-9]+)/, $date, $m) return false; $f = Explode ("/", $format); $d [$f [0]] = $m [1]; $d [$f [1]] = $m [3]; $d [$f [2]] = $m [5]; return checkdate ($d [' mm '], $d [' DD '], $d [' yyyy ']. $d [' yy ']); }
function Check_url ($url) {//check a URL return (BOOL) Preg_match ("/^ (?:(?: HT|F) TP (?: S?):/ /|~/|/)? (:(?: w+.) +) w+ (?::d +)? (?:(?:/ [^/?#]+)+)?/? (?:? [^?] *)? (#.*) $/i ", $url); } } |
Call method
The code is as follows |
Copy Code |
$validation = new validation (); Detecting mailboxes Var_dump ($validation->check_email ("abc@abc.abc.com"));//true Var_dump ($validation->check_email ("abc.abc@abc.com"));//false |
IP detection
The code is as follows |
Copy Code |
Var_dump ($validation->check_ip ("127.0.0.1.0")); Var_dump ($validation->check_ip ("256.0.0.1")); |
Date format detection
The code is as follows |
Copy Code |
True Var_dump ($validation->check_date ("2010/1/20", "YYYY/MM/DD")); Var_dump ($validation->check_date ("1/20/10", "Mm/dd/yy")); Var_dump ($validation->check_date ("2010-20-1", "yyyy/dd/mm")); False Var_dump ($validation->check_date ("2010/2/29", "YYYY/MM/DD")); Var_dump ($validation->check_date ("2010/1/20", "yyyy/dd/mm")); Var_dump ($validation->check_date ("2010-20/1", "yyyy/dd/mm")); |
/*
The use of such a method is very simple, as long as the creation of classes can easily verify the mailbox, date, address, IP is legitimate oh.
*/