2

I simply need to be able to have a text field and validate if input is a decimal number like 100.00, 0.1, 10.6, etc.

I'm using Drupal 7.

Updated question to indicate 'programatic' solution (using Form API).

7

I can't think of a better solution than to use a standard textfield with the element_validate_number() validator:

$form['element'] = array(
  '#type' => 'textfield',
  '#title' => t('Title'),
  '#element_validate' => array('element_validate_number'),
);

If you need something more complicated than the is_numeric() that gets run by element_validate_number() you just need to provide your own validation function, or if you want to mess with the widget itself then create your own element type using hook_element_info()

  • That's what I put in comment before OP clarified it is indeed about Form API. Too bad I wasn't fast enough to post my answer after clarification :P +1 from me anyway. – Mołot Nov 6 '13 at 10:57
1

Under Strucuture->content_type->manage fields : There is field type called Decimal where you can get a textfield having only decimal numbers allowed with validation.

enter image description here

for programmatically adding decimal field you can ,

$field = array(
    'field_name' => abc_decimal,
    'type' => 'number_decimal',
);
  • After question update, your answer does not fit anymore. Well, question was totally unclear, so I guess you was a bit too fast to answer. – Mołot Nov 6 '13 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.