HTMLIntField.php 802 B

123456789101112131415161718192021222324252627
  1. <?php
  2. /**
  3. * A field that must contain a number
  4. */
  5. class HTMLIntField extends HTMLFloatField {
  6. public function validate( $value, $alldata ) {
  7. $p = parent::validate( $value, $alldata );
  8. if ( $p !== true ) {
  9. return $p;
  10. }
  11. # https://www.w3.org/TR/html5/infrastructure.html#signed-integers
  12. # with the addition that a leading '+' sign is ok. Note that leading zeros
  13. # are fine, and will be left in the input, which is useful for things like
  14. # phone numbers when you know that they are integers (the HTML5 type=tel
  15. # input does not require its value to be numeric). If you want a tidier
  16. # value to, eg, save in the DB, clean it up with intval().
  17. if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) ) ) {
  18. return $this->msg( 'htmlform-int-invalid' );
  19. }
  20. return true;
  21. }
  22. }