Showing posts with label regex validation. Show all posts
Showing posts with label regex validation. Show all posts

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));
}

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);
}

Wednesday, 7 September 2016

Email Address Validation in PHP using Regex

Simple regex to validate email address in php. Validate email address in php regex while submitting form.

$email_regex = "/([a-z0-9_]+|[a-z0-9_]+\.[a-z0-9_]+)@(([a-z0-9]|[a-z0-9]+\.[a-z0-9]+)+\.([a-z]{2,4}))/i";
if(!preg_match($email_regex, $email)) {
    echo "Invalid Email Address";
}
else {
    echo "Valid Email Address";
}