Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Extensions
UK Postcodes
Commits
c8b87431
Commit
c8b87431
authored
Mar 29, 2018
by
mattwire
Browse files
Add postcode validation function
parent
41ce81fe
Changes
1
Hide whitespace changes
Inline
Side-by-side
CRM/Civicrmpostcodelookup/Page/Postcode.php
View file @
c8b87431
...
...
@@ -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
;
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment