This is second part of 60 PHP validation functions.
PHP validation functions
- php validation function for alpha characters
- php validation function for alpha dash characters
- php validation function for alpha numeric characters
- php validation function for ascii input string
- php validation function for base64 string
- php validation function for boolean objects
- php validation function for credit card
- php validation function for date
- php validation function for dateDe
- php validation function for dateISO
- php validation function for decimal
- php validation function for email address
- php validation function for email domain exists
- php validation function for empty object
- php validation function for enum oject
- php validation function for variable exists
- php validation function for hexcolor
- php validation function for html tags
- php validation function for integer
- php validation function for ip address
- php validation function for jssafe
- php validation function for string length
- php validation function for lower string
- php validation function for mac address
- php validation function for matches
- php validation function for max length
- php validation function for max value
- php validation function for md5 string
- php validation function for min length
- php validation function for min value
- php validation function for multiline
- php validation function for numeric
- php validation function for phone
- php validation function for pin code
- php validation function for range length
- php validation function for range value
- php validation function for regex
- php validation function for rgb
- php validation function for time12
- php validation function for time24
- php validation function for time zone
- php validation function for token
- php validation function for unique
- php validation function for upper
- php validation function for url
- php validation function for url exists
- php validation function for usssn
- php validation function for utf8
Validation for Javascript injection (jssafe)
Use: There is a chance to hackers to include javascript code in our Input form elements such as Text area ,Text element.It is best practice to avoid such hacking in E-commerce applications.
Javascript Script Regular Expression
/<script[^>]*>[srn]*(<!--)?|(-->)?[srn]*</script>/ |
PHP function
/** * Check given sting has script tags * @param string * @return boolean */ function is_jssafe($val) { return (bool)(!preg_match("/<script[^>]*>[srn]*(<!--)?|(-->)?[srn]*</script>/", $val)); } |
Validation for String length
Use: validate string length.
Regular Expression
We used handy PHP functions here
PHP function
/** * check for exactly length of string * @param string * @return boolean */ function is_length($val, $length) { return (strlen($val) == (int)$length); } |
Validation for lower case String
Use: Oops! in my experiance i didn’t got a chance to validate againest this. But i kept for reference cause it may use full to any one.
Regular Expression
/^[a-z]+$/ |
PHP function
/** * Given sting is lower cased * @param string * @return boolean */ function is_lower($val) { return (bool)preg_match("/^[a-z]+$/", $val); } |
Validate macaddress
Use: In some network related and secure money transfer application ,it best practice to rely on MacAddress than IP address. This can be captured with JAVA applet only
Mac address Regular Expression
/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/ |
PHP function
/** * Checks given value again MAC address of the computer * @param string value * @return boolean */ function is_macaddress($val) { return (bool)preg_match('/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/', $val); } |
Validation for strict Object matches
Use: It is used in DB session handling , we fetch object from serialized input from files, Database ..etc. when unserialise it best practice to validate that object .
Regular Expression
We use PHp comparision
PHP function
/** * compares two any kind of values ,stictly * @param string * @return boolean */ function is_matches($val, $value) { return ($val === $value); } |
Validation for String lenght maximum
Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.
Regular Expression
We use PHP comparision
PHP function
/** * check string length exceeds maximum length * @param string * @return boolean */ function is_maxlength($val, $max) { return (strlen($val) <= (int)$max); } |
Validation for number maximum value
Use: It is most common validation againest numbers
Regular Expression
We use PHP comparision
PHP function
/** * check given number exceeds max values * @param string * @return boolean */ function is_maxvalue($number,$max) { return ($number >$max); } |
Validate md5 String
Use: in mostly case we use md5 for secret data passing and key , password validation.
md5 string Regular Expression
/[0-9a-f]{32}/i |
PHP function
/** * Checks that a field matches a v2 md5 string * @param string * @return boolean */ function is_md5($val) { return (bool)preg_match("/[0-9a-f]{32}/i", $val); } |
Validation for String minimum length
Use: in mostly forms we must validate the input value for maximum length, cause Mysql VARACHAR(255) supports.
PHP function
/** *Check the string length has minimum length * @param string * @return boolean */ function is_minlength($val, $min) { return (strlen($val) >= (int)$min); } |
Validation for number minimum value
Use : mostly forms elements we check this validation, example in registration form we ask user age.
Regular Expression
We use PHP comparision
PHP function
/** * check given number below value * @param string * @return boolean */ function is_minvalue($number) { return ($number < $max); } |
Validation for multy line characters.
Use: some times ,we need to strip or validate the new line characters.
Regular Expression
/[nrt]+/ |
PHP function
/** * check given sring has multilines * @param string * @return boolean */ function is_multiline($val) { return (bool)preg_match("/[nrt]+/", $val); } |
Validation for numeric
Use: validate given input string is numeric with optional +,-,. values
Regular Expression
/^[-+]?[0-9]*.?[0-9]+$/ |
PHP function
/** * check a number optional -,+,. values * @param string * @return boolean */ function is_numeric($val) { return (bool)preg_match('/^[-+]?[0-9]*.?[0-9]+$/', $val); } |
Validate for Phone Number
Use: validate given input is phone number
PHP function
/** * Matches a phone number that length optional numbers 7,10,11 * @param string * @return boolean */ function is_phone($number, $lengths = null) { if (!is_array($lengths)) { $lengths = array(7, 10, 11); } $number = preg_replace('/D+/', '', $number); return in_array(strlen($number), $lengths); } |
Validate Pincode
Use: validate given input is phone number
Regular Expression
We used more than one regular expression depending on countries |
PHP function
/** * Checks that given value matches following country pin codes. * at = austria * au = australia * ca = canada * de = german * ee = estonia * nl = netherlands * it = italy * pt = portugal * se = sweden * uk = united kingdom * us = united states * @param String * @param String * @return boolean */ function is_pincode($val, $country = 'us') { $patterns = array('at' => '^[0-9]{4,4}$', 'au' => '^[2-9][0-9]{2,3}$', 'ca' => '^[a-zA-Z].[0-9].[a-zA-Z].s[0-9].[a-zA-Z].[0-9].', 'de' => '^[0-9]{5,5}$', 'ee' => '^[0-9]{5,5}$', 'nl' => '^[0-9]{4,4}s[a-zA-Z]{2,2}$', 'it' => '^[0-9]{5,5}$', 'pt' => '^[0-9]{4,4}-[0-9]{3,3}$', 'se' => '^[0-9]{3,3}s[0-9]{2,2}$', 'uk' => '^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$', 'us' => '^[0-9]{5,5}[-]{0,1}[0-9]{4,4}$'); if (!array_key_exists($country, $patterns)) return false; return (bool)preg_match("/" . $patterns[$country] . "/", $val); } |
Validation for string lenght is with in range of given lengths
Use : This is the most common validation again user input values.
Regular Expression:
Here we used PHP handy functions.
PHP function
/** * check given string length is between given range * @param string * @return boolean */ function is_rangelength($val, $min = '', $max = '') { return (strlen($val) >= $min and strlen($val) <= $max); } |
Validation for given integer value between given two values
Use : This is the most common validation again user input values.
Regular Expression:
Here we used PHP handy functions.
PHP function
/** * check given number between given values * @param string * @return boolean */ function is_rangevalue($number,$min,$max) { return ($number > $min and $number < $max); } |
Validation input value againest given regex
Use: in some cases we have use custom regular expression.
Regular Expression:
Here we used PHP handy functions.
PHP function
/** * check given number between given values * @param string * @return boolean */ function is_rangevalue($number,$min,$max) { return ($number > $min and $number < $max); } |
Validation for rgb colour value
use: we validate the user input is RGB color
rgb color Regular Expression:
/^(rgb(s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*))|(rgb(s*(d?d%|100%)+s*,s*(d?d%|100%)+s*,s*(d?d%|100%)+s*))$/ |
PHP function
/** * Check is rgb color value * @param string * @return boolean */ function is_rgb($val) { return (bool)preg_match("/^(rgb(s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*,s*b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])bs*))|(rgb(s*(d?d%|100%)+s*,s*(d?d%|100%)+s*,s*(d?d%|100%)+s*))$/", $val); } |
Validate time 12 hours format
use: Validate for 12 hours format time
12 hours format Regular Expression:
/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/ |
PHP function
/** * Time in 12 hours format with optional seconds * 08:00AM | 10:00am | 7:00pm * @param string * @return boolean */ function is_time12($val) { return (bool)preg_match("/^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$/", $val); } |
Validation for 24 hours format time
use: Validate for 24 hours format time
24 hours time format Regular Expression:
/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/ |
PHP function
/** * Time in 24 hours format with optional seconds * 12:15 | 10:26:59 | 22:01:15 * @param string * @return boolean */ function is_time24($val) { return (bool)preg_match("/^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/", $val); } |
Validation for time zone format
use: Validate time zone format GMT
GMT time zone Regular Expression:
/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/ |
PHP function
/** * Checks given value matches a time zone * +00:00 | -05:00 * @param string * @return boolean */ function is_timezone($val) { return (bool)preg_match("/^[-+]((0[0-9]|1[0-3]):([03]0|45)|14:00)$/", $val); } |
Validation for token
use: A token that don’t have white spaces.
Regular Expression:
/s/ |
PHP function
/** * A token that don't have any white space * @param string * @return boolean */ function is_token($val) { return (bool)!preg_match('/s/', $val); } |
Validate unique array elements
Use: in some times we ask user multiple choices ,so these elements are unique are not!!
PHP function
/** * check if array has unique elements,it must have minimum one element * @param string * @return boolean */ function is_unique($arr) { $arr = (array )$arr; $count1 = count($arr); $count2 = count(array_unique($arr)); return (count1 != 0 and (count1 == $count2)); } |
Validation for input value is Upper case
Use: in experience i didn’t got any chance to validate this ,but it is may use full for others.
Regular Expression
/^[A-Z]+$/ |
PHP function
/** * Given string is upper cased? * @param string * @return boolean */ function is_upper($val) { return (bool)preg_match("/^[A-Z]+$/", $val); } |
Validation for URL
Use: validate given input is URL format
Regular Expression
PHP function
/** * Valid URL or web address * @param string * @return boolean */ function is_url($val) { return (bool)preg_match("^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$", $val); } |
Validate URL exists
Use: some times given we need If URL works or not
PHP function
/** * Check given url really exists? * @param string * @return boolean */ function is_urlexists($link) { return (bool)@fsockopen($link, 80, $errno, $errstr, 30); } |
Validation for US Social security number
Use : validate the user input is valid US social security number format
US social security number Regular Expression:
/^d{3}-d{2}-d{4}$/ |
PHP function
/** * Checks given value matches us citizen social security number * @param string * @return boolean */ function is_usssn($val) { return (bool)preg_match("/^d{3}-d{2}-d{4}$/", $val); } |
Validation for utf8
Use: given file input string is UTF8 format
Regular Expression
PHP function
/** * check given sting is UTF8 * @param string * @return boolean */ function is_utf8($val) { return preg_match('%(?: [xC2-xDF][x80-xBF] |xE0[xA0-xBF][x80-xBF] |[xE1-xECxEExEF][x80-xBF]{2} |xED[x80-x9F][x80-xBF] |xF0[x90-xBF][x80-xBF]{2} |[xF1-xF3][x80-xBF]{3} |xF4[x80-x8F][x80-xBF]{2} )+%xs', $val); } |
Use: i have noticed my tutorials are directly copied on other blogs without informing, i spent some man hours doing this , if you want refer please send me back link.

