Skip to main content

Drupal 7's positive integer validator

Ever written custom code to check if your textfield is a positive integer? I sure have. Then I found an easier way.

by Nick Schuch /

In the past I have been required to ensure that a textfield is a positive integer. For these cases I have written a custom element validator and attached it to the field using #element_validate. The below is a paste of the validator rewritten for a custom module called “blog_post”.

/**
 * Custom function to check for positive integer.
 */
function blog_post_integer_validate($element, &$form_state) {
  $value = $element['#value'];
  if (!is_numeric($value) && $value <= 0) {
    form_error($element, t('Please provide a positive integer for %name.', array('%name' => $element['#title'])));
  }
}

After some digging through Drupal 7 core API's I found something even better! I found a validator that does all the heavy lifting for me. This validator is called element_validate_integer_positive. As can be seen it tackles way more edge cases than what mine does and I wish I found it sooner.

function element_validate_integer_positive($element, &$form_state) {
  $value = $element['#value'];
  if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
    form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
  }
}

More on this validator and how to implement can be found here: http://api.drupal.org/api/drupal/includes!form.inc/function/element_validate_integer_positive/7

Posted by Nick Schuch
Sys Ops Lead

Dated

Comments

Comment by Robin Millette

Dated

What about using an Integer textfield and giving it a Minimum value of 0, as can be seen in http://imgbin.org/images/10290.png ? Perhaps I'm missing something...

Comment by Medden

Dated

Rather than checking if an integer is positive, I use a simple php function to convert negative numbers to positive ones.

if ($value !== '' && (!is_numeric($value) {
$value = abs($value);
}

Doesn't work for every use case, but might save you some time.

Comment by Jeff Mahoney

Dated

Thanks :) I've already implemented this in one of my forms!

Comment by Alex

Dated

Answer: B It's valuable to know how to repnesert remainders in algebraic terms. When m is divided by n, there is an integer quotient (in this case, 24), and the decimal part consists of the remainder divided by the denominator. For instance, when 4 is divided by 3, the quotient is 1 and the remainder is 1:4/3 = 1 + 1/3In general terms:m/n = q + r/nWe can ignore the quotient in this problem: We know it's 24. The fractional part, however, is repneserted in two ways. First, it is equal to 0.2. Second, it is equivalent to r/n, or 12/n. We can solve for n by setting those two equal to each other:0.2 = 12/n2/10 = 12/n2n = 120n = 60, choice (B).

Pagination

Add new comment

Restricted HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Not sure where to start? Try typing "hello" or "help" if you get stuck.