Take the 2-minute tour ×

Hey Guys,
i have made module in which i am trying to add validation like if the user had entered the characters in "Phone No" text filed and same on "Mobile No".
This will run when user had open the user registration form.
I have made this....

function form_intro_form_alter($form_id,&$form){
    if($form_id == 'user_register' || $form_id == 'user_edit'){
        $form['Personal Information']['profile_pno']['#validate'] = array('form_intro_pno_validate' => array());   //profile_pno is for Phone No.
        $form['Personal Information']['profile_mno']['#validate'] = array('form_intro_mno_validate' => array());   //profile_mno is for Mobile No.

function form_intro_pno_validate($element){
        form_set_error('profile_pno' , t('Please Enter Only Number in Phone no'));

function form_intro_mno_validate($element){
        form_set_error('profile_mno' , t('Please Enter Only Number in Mobile no'));

the module name is form_intro.....
plz check it and send me replay...
this isn't working...it not giving any error when user had entered the characters.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You need to use #element_validate to pass on a validation handler per element or $form['#validate'] to add a validation handler to the form. That is why it's not working.

Another reason why it doesn't work for you, is that you implemented the hook wrongly. For the hook hook_form_FORM_ID_alter, you need to replace hook with your module's name and FROM_ID with the form id.

An example:

function my_module_form_intro_form_alter(&$form, &$form_state) {
  $form['#validate'][] = 'my_module_form_validation_handler';
  $form['title'] = array(
    '#type' => 'textfield',
    '#description' => t('The title you enter here appears on the page.'),
    '#size' => 40,
    '#maxlength' => 255,
    `#element_validate` => array('my_module_element_validation_handler'),

Doing the above, the function my_module_form_validation_handler will be called for the entire form, while my_module_element_validation_handler will be called for the title form item.

share|improve this answer
sorry but i didn't get u....can you plz explain in some detail.. i am going mad with this thing....are you suggesting me to change the function name or what? –  Nitz May 4 '10 at 13:06
welcome to drupal, #Nitz . Why don't you buy yourself a book or two? –  Your Common Sense May 4 '10 at 13:31
don't want to say any thing to BOOK MAESTRO....Col. Shrapnel..... thanks googletorp...for answer.....i got it.... –  Nitz May 4 '10 at 14:16
add comment

I had tried with hook_user..

    function legalagree_user($op, &$edit, &$user, $category = NULL) {
      switch($op) {
         case 'validate':
          if (!is_numeric($edit['profile_mno'])) {
form_set_error('profile_mno', t('You have to enter only numbers in Mobile No Field.'));


This is working.....

share|improve this answer
add comment

Your Answer


