Hello, friends in my previous post I show you an opencart trick but in this post I will show you all types of php validation script for input fields. Here I create a specific function for every types of validation like email address, name, url, zip code, USA zip code, UK zip code, social security number, international phone number, US phone number, Indian phone number, credit card number, credit card expiry date, integer, float, string etc. The PHP Validation script is a set of validation rules which lets you add server-side validation to your forms quickly and with as little effort as possible. So lets build the php validation script.
Validate an integer
// function to validate an integer function validateInteger($str) { // test if input is an integer, optionally signed return preg_match("/^-?([0-9])+$/", $str); } // result: "Is an integer" echo validateInteger("123456") ? "Is an integer" : "Is not an integer"; // result: "Is not an integer" echo validateInteger("123456.506") ? "Is an integer" : "Is not an integer"; // result: "Is not an integer" echo validateInteger("12a3456.506") ? "Is an integer" : "Is not an integer";
Validate a float
// function to validate a float function validateFloat($str) { // test if input is a floating-point number, optionally signed return preg_match("/^-?([0-9])+([\.|,]([0-9])*)?$/", $str); } // result: "Is a float" echo validateFloat("123456") ? "Is a float" : "Is not a float"; // result: "Is a float" echo validateFloat("123456.506") ? "Is a float" : "Is not a float";
Validate a string
function validateString($str) { // test if input is a string return is_string($str); }
Validate only alphabetic characters
function validateAlpha($str) { // test if input contains only alphabetic characters return preg_match("/^[a-z]+$/i", $str); } // result: "Is alphabetic" echo validateAlpha("abc") ? "Is alphabetic" : "Is not alphabetic"; // result: "Is not alphabetic" echo validateAlpha("abc1") ? "Is alphabetic" : "Is not alphabetic";
Validate alphabetic and numeric characters
function validateAlphaNum($str) { // test if input contains alphabetic and numeric characters return preg_match("/^[a-z0-9]*$/i", $str); } // result: "Is an alphabetic string" echo validateAlphaNum("abc") ? "Is an alphabetic string" :"Is not an alphabetic string"; // result: "Is an alphabetic string" echo validateAlphaNum("abc1") ? "Is an alphabetic string" :"Is not an alphabetic string"; // result: "Is not an alphabetic string" echo validateAlphaNum("abc?") ? "Is an alphabetic string" :"Is not an alphabetic string";
Validate a credit card number
// function to validate a credit card number function validateCCNum($str) { // test if input is of the form dddddddddddddddd return preg_match("/^\d{16}$/" ,$str); } // result: "Is a valid 16-digit number" echo validateCCNum("4476269198125132") ? "Is a valid 16-digit number" :"Is not a 16-digit number";
Validate a credit card expiry date
// function to validate a credit card expiry date function validateCCExpDate($str) { // test if input is of the form mm/yyyy return preg_match("/(0[1-9]|1[0-2])\/20[0-9]{2}$/", $str); } // result: "Is a valid date string" echo validateCCExpDate("12/2013") ? "Is a valid date string" :"Is an invalid date string";
Validate an Indian phone number
// function to validate an Indian phone number function validateIndiaPhone($str) { // test if input is of the form (0aa) nnnn nnnn return preg_match("/^\(0\d{2}\)\s?\d{8}$/", $str); } // result: "Is a properly-formatted phone number" echo validateIndiaPhone("(022) 22881111") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number";
Validate US phone number
// function to validate a US phone number function validateUSPhone($str) { // test if input is of the form aaa-nnn-nnnn return preg_match("/^[2-9]\d{2}-\d{3}-\d{4}$/", $str); } // result: "Is a properly-formatted phone number" echo validateUSPhone("301-111-1111") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number";
Validate international phone number
// function to validate an international phone number function validateIntlPhone($str) { // test if input is of the form +cc aa nnnn nnnn return preg_match("/^(\+|00)[1-9]{1,3}(\.|\s|-)?([0-9]{1,5}(\.|\s|-)?){1,3}$/", $str); } // result: "Is a properly-formatted phone number" echo validateIntlPhone("+1 301 111 1111") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number"; // result: "Is a properly-formatted phone number" echo validateIntlPhone("0091-11-2123-7574") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number"; // result: "Is a properly-formatted phone number" echo validateIntlPhone("+612 9555-5555") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number"; // result: "Is an improperly-formatted phone number" echo validateIntlPhone("12346") ? "Is a properly-formatted phone number" : "Is an improperly-formatted phone number";
Validate U.S. Social Security number
// function to validate U.S. Social Security number function validateSSN($str) { // test if input is of the form ddd-dd-dddd return preg_match("/^\d{3}\-\d{2}\-\d{4}$/", $str); } // result: "Is a properly-formatted SSN" echo validateSSN("123-45-6789") ? "Is a properly-formatted SSN" :"Is an improperly-formatted SSN"; // result: "Is an improperly-formatted SSN" echo validateSSN("123456789") ? "Is a properly-formatted SSN" :"Is an improperly-formatted SSN";
Validate a zip code
// function to validate a zip code function validateZip($str) { // test if input is of the form dddddd return preg_match("/^\d{6}$/" ,$str); } // result: "Is a properly-formatted zip code" echo validateZip("123456") ? "Is a properly-formatted zip code" :"Is an improperly-formatted zip code";
Validate UK zip code
// function to validate a UK zip code function validateUKZip($str) { // test if input is of the form ssdd dss return eregi("^[a-z]{1,2}[0-9]{1,2}([a-z])?[[:space:]]?[0-9][a-z]{2}$" ,$str); } // result: "Is a properly-formatted UK postcode" echo validateUKZip("NW3 5ED") ? "Is a properly-formatted UK postcode" :"Is an improperly-formatted UK postcode";
Validate a US zip code
// function to validate a US zip code function validateUSZip($str) { // test if input is of the form dddd-ddddd return preg_match("/^\d{5}(-\d{4})?$/" ,$str); } // result: "Is a properly-formatted US zip code" echo validateUSZip("10113-1243") ? "Is a properly-formatted US zip code" : "Is an improperly-formatted US zip code";
Validate an e-mail address
// function to validate an e-mail address function validateEmailAddress($str) { // test if input matches e-mail pattern return eregi("^([a-z0-9_-])+([\.a-z0-9_-])*@([a-z0-9-])+.(\.[a-z0-9-]+)*\.([a-z]{2,6})$", $str); } // result: "Is a properly-formatted e-mail address" echo validateEmailAddress("joe@some.domain.com") ? "Is a properly-formatted e-mail address" : "Is an improperly-formatted e-mail address"; // result: "Is an improperly-formatted e-mail address" echo validateEmailAddress("joe@dom.") ? "Is a properly-formatted e-mail address" : "Is an improperly-formatted e-mail address";
Validate a URL
// function to validate a URL function validateUrl($str) { // test if input matches URL pattern return preg_match("/^(http|https|ftp):\/\/([a-z0-9]([a-z0-9_-]*.[a-z0-9])?\.)+[a-z]{2,6}\/?([a-z0-9\?\._-~&#=+%]*)?/", $str); } // result: "Is valid" echo validateUrl("http://www.example.com/html/index.php") ? "Is valid" : "Is invalid"; // result: "Is valid" echo validateUrl("http://www.some.site.info") ? "Is valid" : "Is invalid"; // result: "Is invalid" echo validateUrl("http://some") ? "Is valid" : "Is invalid";
Validate an IP address
// function to validate an IP address function validateIP($str) { if(!filter_var($ip, FILTER_VALIDATE_IP)) { return false; } else { return true; } } // result: "Is valid" echo validateIP("192.168.0.1") ? "Is valid" : "Is invalid"; //result: "Is Invalid" echo validateIP("192.168") ? "Is valid" : "Is invalid";
Thank you for viewing my blog. Please comment and share if you like my post.