FormValidator.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /** @package verysimple::HTTP */
  3. /** import supporting libraries */
  4. /**
  5. * Static utility class for validating form input
  6. *
  7. * Contains various methods for validating standard information such
  8. * as email, dates, credit card numbers, etc
  9. *
  10. * @package verysimple::HTTP
  11. * @author VerySimple Inc.
  12. * @copyright 1997-2008 VerySimple, Inc. http://www.verysimple.com
  13. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  14. * @version 1.0
  15. */
  16. class FormValidator
  17. {
  18. /**
  19. * Returns true if the provided email is valid
  20. *
  21. * @param string email address
  22. * @return bool
  23. */
  24. static function IsValidEmail($email)
  25. {
  26. return (
  27. eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)
  28. );
  29. }
  30. /**
  31. * Returns true if the provided credit card appears to be valid. If type is
  32. * provided, then the validation makes sure it is valid for that specific
  33. * type, otherwise it just makes sure it is valid for any type
  34. *
  35. * @param string credit card number
  36. * @param string type [optional] (American, Dinners, Discover, Master, Visa)
  37. * @return bool
  38. */
  39. static function IsValidCreditCard($cc_num, $type = "")
  40. {
  41. if($type == "American") {
  42. $denum = "American Express";
  43. } elseif($type == "Dinners") {
  44. $denum = "Diner's Club";
  45. } elseif($type == "Discover") {
  46. $denum = "Discover";
  47. } elseif($type == "Master") {
  48. $denum = "Master Card";
  49. } elseif($type == "Visa") {
  50. $denum = "Visa";
  51. }
  52. $verified = false;
  53. if($type == "American" || $type == "") {
  54. $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
  55. if (preg_match($pattern,$cc_num)) {
  56. $verified = true;
  57. }
  58. }
  59. if($type == "Dinners" || $type == "") {
  60. $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
  61. if (preg_match($pattern,$cc_num)) {
  62. $verified = true;
  63. }
  64. }
  65. if($type == "Discover" || $type == "") {
  66. $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
  67. if (preg_match($pattern,$cc_num)) {
  68. $verified = true;
  69. }
  70. }
  71. if($type == "Master" || $type == "") {
  72. $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
  73. if (preg_match($pattern,$cc_num)) {
  74. $verified = true;
  75. }
  76. }
  77. if($type == "Visa" || $type == "") {
  78. $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
  79. if (preg_match($pattern,$cc_num)) {
  80. $verified = true;
  81. }
  82. }
  83. return $verified;
  84. }
  85. }
  86. ?>