Showing posts with label PHP Validation. Show all posts
Showing posts with label PHP Validation. Show all posts

Sunday, 18 September 2016

PHP Validation Function for Maximum String Length

Useful PHP form validation function to validate the length of the string against the maximum allowed string length which in default has a maximum length of 10. Use this validation function to validate the string length of a string.

function validateMaxLength($allowed_length, $allowed_length=10) {
    return (strlen($allowed_length) <= (int)$allowed_length);
}

Saturday, 17 September 2016

PHP Function to Validate URLs

Usage of simple URL validation function in php. This simple function helps in validation of URL for correct format, this also validates for mailto links in the content.

function validateURL($url) {
    return (bool)preg_match("^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$",$url);
}

Friday, 16 September 2016

Check for Script Tags in String using PHP Functions

In order to make hacking attempts fail we need to make our input contents safe and secure. Here to avoid unwanted issue due to script injection we make the content/string safe of script tags. Thus script hacking cannot be done in our contents.

function validateJsScriptSafe($value) {
    return (bool)(!preg_match("/<script[^>]*>[srn]*(<!--)?|(-->)?[srn]*</script>/",$value));
}

Thursday, 15 September 2016

PHP Validation Function for HTML Safe

Simple php function to validate html tags in given content. In some cases we need to check if the given string contains html tags in order to avoid hacking attends and also to avoid unwanted elements changing the layout of our site.

function validateHtmlSafe($value) {
    return (bool)(!preg_match("/<(.*)>.*<$1>/", $value));
}

Tuesday, 13 September 2016

PHP Function to Validate Hex Color Code

PHP function to validate the given value for valid hex color code. Validate using this simple php function for valid hex color code.

function validateHexColor($color_code) {
    return (bool)preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color_code);
}

Sunday, 11 September 2016

PHP Function to Validate Alpha Characters

Validate alpha characters using php snippets. This function allows only alphabets either in small letters or in capital letters. Php regex that allows only alpha characters.

function validateAlpha($string) {
    return (bool)preg_match("/^([a-zA-Z])+$/i", $string);
}

Saturday, 10 September 2016

Validate MAC Address in PHP

Simple snippet to validate MAC address in php. This functions receives mac address as input and returns a boolean value based on the check. If mac address is not valid function will return false or if valid it will return true.

function validateMacAddress($mac_address)
    return (bool)preg_match('/^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/', $mac_address);
}