Form validation: text has to be a number I have a textfield in a form that must contain a number. Using an integer for the field type is not possible - it must be text; however, I need to ensure that the input is actually a number. I've used:

function myfunc_form_user_register_form_validate($form, &amp;$form_state) {
  if (!is_numeric($form_state['values']['number'])) {
    form_set_error('number', t('Field must be Numeric'));
  }
}


However, it does not work, that is, no errors are ever displayed. I have implemented the above code in template.php. I know there are Form Validation modules; however, I would like to do such a simple thing myself. I also think it's a bit overkill to create a whole module to simply validate a single field in a single form! I also wonder how to find the $form[] keys. I named the input field number so I imagine that's the key but it's just a guess. I've looked through the Form API and it is not very clear what I am supposed to do. Simple things, such as finding out what's even in $form[] alludes me and it doesn't seem to be clearly explained.
Drupal already has a helper for this, in the shape of the element_validate_number() validation callback:

function MYMODULE_form_user_register_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  $form['number']['#element_validate'][] = 'element_validate_number';
}


You can also use element_validate_integer() or element_validate_integer_positive if either of those make more sense for your use case.