String Validation
Validate name, surname by the below function to restrict only letters and spaces.
function validateString($str) { //accept letters and space only return preg_match('/^[A-Za-zs ]+$/', $str); }
Validate numeric value by the below function to restrict only numbers,you can uncomment any line according to your need.
function validateNumber($value) { //is_double($value); //is_float($value); //is_int($value); //is_integer($value); return is_numeric($value); }
Alphanumeric Characters Validation
Below function validates alphanumeric characters.ctype is complete function library in PHP.
function validateAlphanumeric($string) { return ctype_alnum ($string); }
Validation for URL Exist or not
This function will check whether a given URL exist and not only validate it.
function urlExist($url) { $url = @parse_url($url); if (!$url){ return false; } $url = array_map('trim', $url); $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port']; $path = (isset($url['path'])) ? $url['path'] : ''; if ($path == ''){ $path = '/'; } $path .= (isset($url['query'])) ? '?$url[query]' : ''; if (isset($url['host']) AND $url['host'] != @gethostbyname($url['host'])) { if (PHP_VERSION >= 5){ $headers = @get_headers('$url[scheme]://$url[host]:$url[port]$path'); }else{ $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30); if (!$fp){ return false; } fputs($fp, 'HEAD $path HTTP/1.1rnHost: $url[host]rnrn'); $headers = fread($fp, 4096); fclose($fp); } $headers = (is_array($headers)) ? implode('n', $headers) : $headers; return (bool)preg_match('#^HTTP/.*s+[(200|301|302)]+s#i', $headers); } return false; }
Validation for check Image Exist
Below function will check whether a given image link exist and not.
function imageExist($url) { if(@file_get_contents($url,0,NULL,0,1)){return 1;}else{ return 0;} }
IP Address Validation
function will validate an IP address.
function validateIP($IP){ return preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP) }
Validation for Proxy
This function will detect proxy visitors even those that are behind anonymous proxy 🙂
function validateProxy(){ if ($_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['HTTP_X_FORWARDED'] || $_SERVER['HTTP_FORWARDED_FOR'] || $_SERVER['HTTP_VIA'] || in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554)) || @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30)) { exit('Proxy detected'); } }
Strong Password Validation
Check whether a particular password filled by the user is strong enough or not.
function validatePassword($password){ //must contain 8 characters, 1 uppercase, 1 lowercase and 1 number return preg_match('/^(?=^.{8,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/', $password); }
Credit Card Validation
Below function validate credit card number format.
function validateCreditCard($cc){ //eg. 746486033718121 return preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc); }