Changeset 42 for trunk/lib/FormValidator.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FormValidator.inc.php
r41 r42 1 1 <?php 2 2 /** 3 * FormValidator.inc.php 3 * FormValidator.inc.php 4 4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information 5 5 * … … 25 25 class FormValidator 26 26 { 27 27 28 28 /** 29 29 * Array filling with errors. The key will be the name of the form where … … 31 31 */ 32 32 var $errors = array(); 33 33 34 34 /** 35 35 * Return the current list of errors. … … 43 43 return $this->errors; 44 44 } 45 45 46 46 /** 47 47 * Add an error to the errors stack. … … 64 64 ); 65 65 } 66 66 67 67 /** 68 68 * Check whether any errors have been triggered. … … 70 70 * @param string $form_name the name of the incoming form variable 71 71 * 72 * @return bool true if any errors were found, or if found for 72 * @return bool true if any errors were found, or if found for 73 73 * a variable of $form_name, false otherwise 74 74 */ … … 78 78 foreach ($this->errors as $err) { 79 79 if ($err['name'] == $form_name) { 80 return true; 80 return true; 81 81 } 82 82 } … … 115 115 echo '<div class="error">' . $err['message'] . '</div>'; 116 116 break; 117 117 118 118 case MSG_WARNING: 119 119 echo '<div class="warning">' . $err['message'] . '</div>'; 120 120 break; 121 121 122 122 case MSG_SUCCESS: 123 123 echo '<div class="success">' . $err['message'] . '</div>'; 124 124 break; 125 125 126 126 case MSG_NOTICE: 127 127 default: … … 134 134 } 135 135 } 136 136 137 137 /** 138 138 * If this form has an error, print an error marker like "<<". … … 164 164 function notEmpty($form_name, $msg='') 165 165 { 166 166 167 167 $val = trim(getFormData($form_name)); 168 168 if ($val != '') { … … 184 184 function isEmpty($form_name, $msg='') 185 185 { 186 186 187 187 $val = trim(getFormData($form_name)); 188 188 if ($val == '') { … … 255 255 /** 256 256 * Check whether input is a float. Don't just use is_float() because the 257 * data coming from the user is *really* a string. Integers will also 257 * data coming from the user is *really* a string. Integers will also 258 258 * pass this test. 259 259 * … … 293 293 } 294 294 } 295 295 296 296 /** 297 297 * Check whether input matches the specified perl regular expression 298 * pattern. 298 * pattern. 299 299 * 300 300 * @param string $form_name the name of the incoming form variable … … 325 325 } 326 326 } 327 327 328 328 /** 329 329 * Tests if the string length is between specified values. Whitespace excluded for min. … … 339 339 { 340 340 $val = getFormData($form_name); 341 341 342 342 if (strlen(trim($val)) < $min || strlen($val) > $max) { 343 343 $this->addError($form_name, $msg); … … 375 375 /** 376 376 * Validates an email address based on the recommendations in RFC 3696. 377 * Is more loose than restrictive, to allow the many valid variants of 377 * Is more loose than restrictive, to allow the many valid variants of 378 378 * email addresses while catching the most common mistakes. 379 379 * http://www.faqs.org/rfcs/rfc822.html … … 403 403 return false; 404 404 } 405 405 406 406 // We have a match! Here are the captured subpatterns, on which further tests are run. 407 407 $local = $e_parts[2]; … … 415 415 return false; 416 416 } 417 417 418 418 // Check domain exists: It's a domain if ip2long fails; Checkdnsrr ensures a MX record exists; Gethostbyname() ensures the domain exists. 419 419 if (ip2long($domain) == '-1' && function_exists('checkdnsrr') && !checkdnsrr($domain . '.', 'MX') && gethostbyname($domain) == $domain) { … … 422 422 return false; 423 423 } 424 424 425 425 return true; 426 426 } … … 431 431 // return false; 432 432 // } 433 // 433 // 434 434 // // Test email address format. 435 435 // if ($allow_fullname) { … … 444 444 // } 445 445 // } 446 // 446 // 447 447 // // Test length. 448 448 // if (!$this->stringLength($form_name, 0, 255, sprintf(_("<strong>Email address</strong> must contain less than 256 characters."), $email))) { … … 450 450 // return false; 451 451 // } 452 // 452 // 453 453 // // Check domain exists and has valid MX record. 454 454 // preg_match('/^[^<>@]*<?[^\s@\[\]<>()]+\@([A-Za-z0-9.-]+\.[A-Za-z]{2,5})>?$/i', $email, $matches); … … 460 460 // } 461 461 // } 462 // 462 // 463 463 // return true; 464 464 // } … … 476 476 { 477 477 $phone = getFormData($form_name); 478 478 479 479 return $this->checkRegex($form_name, '/^[0-9 +().-]*$/', true, sprintf(_("The phone number <strong>%s</strong> is not valid."), $phone)) 480 480 && $this->stringLength($form_name, 0, 25, sprintf(_("The phone number <strong>%s</strong> is too long"), $phone)); … … 499 499 } 500 500 } 501 502 501 502 503 503 /** 504 504 * Verifies credit card number. … … 515 515 $cc_num = getFormData($form_name); 516 516 } 517 517 518 518 if ('' == $cc_num) { 519 519 return false; 520 520 } 521 521 522 522 // Innocent until proven guilty 523 523 $card_is_valid = true; 524 524 525 525 // Get rid of any non-digits 526 526 $cc_num = preg_replace('/[^\d]/', '', $cc_num); 527 527 528 528 // Perform card-specific checks, if applicable 529 529 switch (strtolower($cc_type)) { … … 558 558 break; 559 559 } 560 560 561 561 // The Luhn formula works right to left, so reverse the number. 562 562 $cc_num = strrev($cc_num); 563 563 564 564 $luhn_total = 0; 565 565 … … 573 573 $digit *= 2; 574 574 } 575 575 576 576 // If the result is two digits, add them. 577 577 if (strlen($digit) == 2) { 578 578 $digit = substr($digit,0,1) + substr($digit,1,1); 579 579 } 580 580 581 581 // Add the current digit to the $luhn_total. 582 582 $luhn_total += $digit; 583 583 } 584 584 585 585 // If it passed (or bypassed) the card-specific check and the Total is evenly divisible by 10, it's cool! 586 586 if ($card_is_valid && $luhn_total % 10 == 0) { … … 609 609 } 610 610 } 611 611 612 612 } // THE END 613 613
Note: See TracChangeset
for help on using the changeset viewer.