Creating Number fields (integer and decimal) manually in code for custom forms I need to manually render a number field in Drupal 7 on a custom form. I have created the following field definition:

$form['num_seats'] = array(
  '#type' =&gt; 'textfield',
      '#title' =&gt; t('Number of Seats'),
      '#required' =&gt; TRUE,
      '#number_type' =&gt; 'decimal',
      '#field_name' =&gt; 'num_seats',
      '#element_validate' =&gt; array ('number_field_widget_validate'),
      '#field_parents' =&gt; array(),
      '#language' =&gt; $language-&gt;language,
      '#field_prefix' =&gt; '$',
      '#size' =&gt; 14,
      '#maxlength' =&gt; 12,
      '#default_value' =&gt; $location ? $location['num_seats'] : $node-&gt;field_workshop_num_seats[$node-&gt;language][0]['value'],
);


However submitting the form produces an error of the form:
"preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 14 in number_field_widget_validate() (line 391 "

If I use 'integer' instead of 'decimal' for 'number_type', it works fine.
I checked Number module's code and could see this is bcause of regex not being created correctly from field definition.

So my question is how can I manually render Fields outside of regular Drupal entity_type context (i.e. outside node/user etc. forms) in a custom form whose submission gets stored to a custom table.
After some googling and a bit of thought of how I've used stuff in the past. You're confusing a FAPI element with a Field Widget. They are not the same thing, this has been discussed before on Drupal.StackExchange. And is fully answered there in terms of why they are different.

I think your easiest solution is to create your own #element_validate function and not rely on the validate function provided directly by number_field_widget_validate -- but I would copy its guts essentially and create your own validate function from it. The preg_replace is failing because your FAPI element is not a Widget and has no $field['settings']. That's what I think is going on.