Home » Validate » Validate_UK » Bug #6049
major errors, incomplete test
Details
| Submitted | 2005-11-23 14:56 UTC |
|---|---|
| From | arpad at rajeczy dot com |
| Assigned | arpad |
| Status | Closed |
| Package | Validate_UK |
| PHP Version | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2005-11-23 14:56 UTC] arpad at rajeczy dot com
Description:
------------
Several of the PCRE patterns are missing the ^$ characters to anchor the pattern to the beginning and end of the input string, so they're not much use for validation.
The pattern for the drive() function is also missing the delimiters, which causes the the pattern to always fail, and throws a warning. The phpt test is severely lacking, it didn't reveal any of these problems.
The patch below should fix the above. The function to check car registration is also a bit dodgy to say the least, but I don't really know where to start with that.
Test script:
---------------
Index: Validate/Validate/UK.php
===================================================================
RCS file: /repository/pear/Validate/Validate/UK.php,v
retrieving revision 1.21
diff -u -r1.21 UK.php
--- Validate/Validate/UK.php 2 Nov 2005 12:16:17 -0000 1.21
+++ Validate/Validate/UK.php 23 Nov 2005 14:45:42 -0000
@@ -125,7 +125,7 @@
// must be in format nn-nn-nn (must contain dashes)
// need to research the range of values - i have assumed 00-00-00 to 99-99-99
// but it might be something like 01-01-01 to 50-99-99
- $preg = "/[0-9]{2}\-[0-9]{2}\-[0-9]{2}/";
+ $preg = "/^[0-9]{2}\-[0-9]{2}\-[0-9]{2}$/";
$match = (preg_match($preg, $sc)) ? true : false;
return $match;
}
@@ -145,7 +145,7 @@
// FIXME *THIS IS PROBABLY WRONG!!! RESEARCH*
// There is a modulus 10/11 system that could be implmeneted here, but it's potentially quite
// complex - http://en.wikipedia.org/wiki/Luhn_formula - Ian
- $preg = "/[0-9]{6,8}/";
+ $preg = "/^[0-9]{6,8}$/";
$match = (preg_match($preg, $ac)) ? true : false;
return $match;
}
@@ -203,7 +203,7 @@
/**
*
* Validates a UK passport number, EU might be the same
- * just checks for 9 digits mine starts 00 and i have included that - might cause problems
+ * just checks for 9 digits
*
* @access public
* @author Michael Dransfield <mikeNO@SPAMblueroot.net>
@@ -214,9 +214,7 @@
function passport($pp)
{
// just checks for 9 digit number
- $preg = "/[0-9]{9}/";
- $match = (preg_match($preg, $pp)) ? true : false ;
- return $match;
+ return (ctype_digit($pp) && strlen($pp) == 9);
}
/**
@@ -230,7 +228,8 @@
*/
function drive($dl)
{
- $preg = "[A-Z]{5}[0-9]{6}[A-Z0-9]{5}";
+ $dl = strtoupper(str_replace(' ', '', $dl));
+ $preg = "/^[A-Z]{5}[0-9]{6}[A-Z0-9]{5}$/";
$match = (preg_match($preg, $dl)) ? true : false;
return $match;
}
Index: Validate/tests/validate_UK.phpt
===================================================================
RCS file: /repository/pear/Validate/tests/validate_UK.phpt,v
retrieving revision 1.2
diff -u -r1.2 validate_UK.phpt
--- Validate/tests/validate_UK.phpt 7 May 2005 15:23:21 -0000 1.2
+++ Validate/tests/validate_UK.phpt 23 Nov 2005 14:43:17 -0000
@@ -67,8 +67,35 @@
'345676', // NOK
'0-78-56', // NOK
'21-68-78', // OK
+ 'foo21-68-78', // NOK
+ '21-68-78bar', // NOK
'34-234-56'); // NOK
+$telNumbers = array(
+ '02012345678', // OK
+ '020-1234-5678', // OK
+ '020 1234 5678', // OK
+ '000 1234 5678', // NOK
+ '020 1234 56789', // OK
+ '020 1234 567', // NOK
+ 'foo020 1234 5678', // NOK
+ '020 1234 5678bar', // NOK
+);
+
+$accountNumbers = array(
+ '01234567', // OK
+ '012345678', // NOK
+ 'foo01234567', // NOK
+ '01234567bar', // NOK
+ 'foobar'); // NOK
+
+$drivingLicences = array(
+ 'ABCDE012345ABCDE', // OK
+ 'ABCDEE012345ABCDE', // NOK
+ 'ABCDE012345ABCD', // NOK
+ 'fooABCDE012345ABCDE', // NOK
+ 'ABCDE012345ABCDEbar'); // NOK
+
echo "Test postalCode\n";
foreach ($postalCodes as $postalCode) {
echo "{$postalCode}: ".$noYes[Validate_UK::postalCode($postalCode)]."\n";
@@ -83,6 +110,21 @@
foreach ($sortCodes as $sortCode) {
echo "{$sortCode}: ".$noYes[Validate_UK::sortCode($sortCode)]."\n";
}
+
+echo "\nTest tel\n";
+foreach ($telNumbers as $v) {
+ echo "{$v}: ".$noYes[Validate_UK::tel($v)]."\n";
+}
+
+echo "\nTest bankAC\n";
+foreach ($accountNumbers as $v) {
+ echo "{$v}: ".$noYes[Validate_UK::bankAC($v)]."\n";
+}
+
+echo "\nTest drive\n";
+foreach ($drivingLicences as $v) {
+ echo "{$v}: ".$noYes[Validate_UK::drive($v)]."\n";
+}
?>
--EXPECT--
Test Validate_UK
@@ -134,4 +176,30 @@
345676: NO
0-78-56: NO
21-68-78: YES
+foo21-68-78: NO
+21-68-78bar: NO
34-234-56: NO
+
+Test tel
+02012345678: YES
+020-1234-5678: YES
+020 1234 5678: YES
+000 1234 5678: NO
+020 1234 56789: YES
+020 1234 567: NO
+foo020 1234 5678: NO
+020 1234 5678bar: NO
+
+Test bankAC
+01234567: YES
+012345678: NO
+foo01234567: NO
+01234567bar: NO
+foobar: NO
+
+Test drive
+ABCDE012345ABCDE: YES
+ABCDEE012345ABCDE: NO
+ABCDE012345ABCD: NO
+fooABCDE012345ABCDE: NO
+ABCDE012345ABCDEbar: NO