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, &$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.
myfunc_form_user_register_form_validate
has been invoked?$form['#validate'][] = "myfunc_form_user_register_form_validate";
2) Yes, it is being called.$form_state['values']['number']
in that case - as far as PHP is concerned, the string doesn't contain a valid number (that's the only wayis_numeric
would fail)form_alter
method is running ($form['#validate'][] = "myfunc_form_user_register_form_validate";