Commit 11368561 authored by mattwire's avatar mattwire
Browse files

Add postcode validation function

parent 41ce81fe
......@@ -23,6 +23,8 @@ abstract class CRM_Civicrmpostcodelookup_Page_Postcode extends CRM_Core_Page {
* @return string
*/
protected static function format($postcode, $space) {
// Convert to uppercase
$postcode = strtoupper($postcode);
// Strip non-alpha characters
$postcode = preg_replace('/\W/', '', $postcode);
if (strlen($postcode) > 4) {
......@@ -32,4 +34,16 @@ abstract class CRM_Civicrmpostcodelookup_Page_Postcode extends CRM_Core_Page {
return $postcode;
}
/**
* Is the provided postcode valid?
* @param $postcode
*
* @return bool
*/
protected static function isValidPostcode($postcode) {
// Regex provided by UK Gov (https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom)
$valid = (boolean)preg_match('/^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})$/', $postcode);
return $valid;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment