cvs: pear /Validate Validate.php
| From: | Tomas V.V.Cox | Date: | Thu, 30 May 2002 14:05:04 +0000 |
| Subject: | cvs: pear /Validate Validate.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4294@lists.php.net to get a copy of this message | ||
cox Thu May 30 10:05:04 2002 EDT
Modified files:
/pear/Validate Validate.php
Log:
Added creditCard() (Validate a number according to Luhn check algorithm)
Contribution by Ondrej Jombik <nepto@pobox.sk>
# I've changed the original intention of Ondrej to have luhn() and
# creditCard(). Take 100 pages from Internet with forms and do some
# stats of what they need to validate, that's the scope of this class.
# Feel free to blame me :-)
Index: pear/Validate/Validate.php
diff -u pear/Validate/Validate.php:1.9 pear/Validate/Validate.php:1.10
--- pear/Validate/Validate.php:1.9 Wed May 29 10:47:06 2002
+++ pear/Validate/Validate.php Thu May 30 10:05:04 2002
@@ -1,4 +1,9 @@
<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// PHP/PEAR header goes here
+//
+// $Id: Validate.php,v 1.10 2002/05/30 14:05:04 cox Exp $
+
/**
Experimental
*/
@@ -98,7 +103,7 @@
}
}
*/
-
+
function url($url, $domain_check = false)
{
if ( is_array( $url ) )
@@ -118,7 +123,39 @@
return false;
}
-
+ /**
+ * Validate a number according to Luhn check algorithm
+ *
+ * This function checks given number according Luhn check
+ * algorithm. It is published on several places, also here:
+ *
+ * http://www.webopedia.com/TERM/L/Luhn_formula.html
+ * http://www.merriampark.com/anatomycc.htm
+ * http://hysteria.sk/prielom/prielom-12.html#3
(Slovak language)
+ * http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html
(Perl lib)
+ *
+ * @param string $number number
+ * @return bool true if number is valid, otherwise false
+ */
+ function creditCard($number)
+ {
+ if (empty($number) || ($len_number = strlen($number)) <= 0) {
+ return false;
+ }
+ $sum = 0;
+ for ($k = $len_number % 2; $k < $len_number; $k += 2) {
+ if ((intval($number[$k]) * 2) > 9) {
+ $sum += (intval($number[$k]) * 2) - 9;
+ } else {
+ $sum += intval($number[$k]) * 2;
+ }
+ }
+ for ($k = ($len_number % 2) ^ 1; $k < $len_number; $k += 2) {
+ $sum += intval($number[$k]);
+ }
+ return $sum % 10 ? false : true;
+ }
+
/*
To Do :
External calls based on commented Date methods