Customize user registration form by adding/modifying fields in the form and handling submit function I am new to drupal.

I need to customize a registration form by adding fields like id,mobile etc.

Can I accomplish this by creating a custom module?

If yes could anyone please help me with a brief idea on creating and overriding the default user registration submit function. I have to insert these details to another table and also have to pass the data as a service request.

Ive created a custom module with function

function module_form_alter(&amp;$form,&amp;$form_state,$form_id){
$form['#submit'] = 'module_form_submit';

if($form_id == 'user_register_form'){ 
    //print_r($form_id);
    $form['email'] = array(
            '#type' =&gt; 'textfield',
          '#title' =&gt; t('id'),
          '#default_value' =&gt; '',
          '#size' =&gt; 60,
          '#maxlength' =&gt; 15,
          '#required' =&gt; TRUE,
     );
    }
}

function module_form_submit($form, &amp;$form_state){
  echo "test";
 exit();
}


module_form_alter is being called and I can see the new field on the registration screen but the submit function is still not called. I need to override the default drupal register submit.

I already have the following function in my theme template.php

function templatename_theme() {
  $items = array();

  $items['user_login'] = array(
    'render element' =&gt; 'form',
    'path' =&gt; drupal_get_path('theme', 'portal') . '/templates',
    'template' =&gt; 'user-login',
    'preprocess functions' =&gt; array(
     'portal_preprocess_user_login'
    ),
  );
  $items['user_register_form'] = array(
    'render element' =&gt; 'form',
    'path' =&gt; drupal_get_path('theme', 'portal') . '/templates',
    'template' =&gt; 'user-register-form',
    'preprocess functions' =&gt; array(
      'portal_preprocess_user_register_form'
    ),
  );
  $items['user_pass'] = array(
    'render element' =&gt; 'form',
    'path' =&gt; drupal_get_path('theme', 'portal') . '/templates',
    'template' =&gt; 'user-pass',
    'preprocess functions' =&gt; array(
      'portal_preprocess_user_pass'
    ),
  );

  return $items;
}


user-register-form.tpl.php

    &lt;div class="form-group"&gt;
    &lt;?php print drupal_render_children($form); ?&gt;
    &lt;/div&gt;


And page--user--register.tpl.php with the html

&lt;div id="login-page"&gt;
    &lt;div class="container"&gt;     
             &lt;div class="form-login" &gt;
            &lt;h2 class="form-login-heading"&gt;&lt;a href="&lt;?php echo $home; ?&gt;"&gt;&lt;img src="&lt;?php echo drupal_get_path('theme', 'portal') . '/images/logo.png'; ?&gt;"  width="100"&gt;&lt;/a&gt;&lt;?php echo $createaccount; ?&gt;&lt;/h2&gt;
            &lt;div class="login-wrap"&gt;
            &lt;?php  
            $elements = drupal_get_form("user_register_form"); 
            $form = drupal_render($elements);
            echo  $form ?&gt;

     &lt;?php if ($messages):?&gt;
        &lt;div id="messages-console" class="clearfix"&gt;
            &lt;div class="grid_12"&gt;
                &lt;div class="mt-grid-fix"&gt;
                &lt;?php print $messages; ?&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;?php endif; ?&gt;



        &lt;/div&gt;
                &lt;/div&gt;

    &lt;/div&gt;
  &lt;/div&gt;


Is this approach fine or should I change this approach inorder to make my custom module functional?

One problem, I need to validate this form in client side, Ive added js in module_form_user_register_form_alter function as
         $form['#attached']['js'] = array(drupal_get_path('module', 'modulenamne').'/js/myjs.js',); 

File is loaded but when clicked on submit the js is not working, instead the page is loading and drupal submit is executing.
If it is not crucial for you to have the additional fields in custom code, I would highly recommend you to make use of the fact that user is a fieldable entity and you can add additional fields easily to it via administration at "admin/config/people/accounts/fields" - this way you do not need to handle saving of the data and display of form elements.

When doing so, you can still make use of custom validation or submit handlers or fiddle with the form and fields easily using the same technique you are using - hook_form_alter and add your submit handler to do whatever logic you require.

Back to your approach — the reason why your submit function is not being called is that you are assigning a string to it instead of adding an element to the array. Try this:

$form['#submit'][] = 'module_form_submit'; (notice the empty brackets on the left side).

If you want to completely overwrite the submit functions array use this:

$form['#submit'] = array('module_form_submit'); - but this will cause that the user will not be created and data saved (unless you do that by hand in your custom submit handler), this also cancels whatever some contrib modules to with the registration form, so you might have unexpected issues.

Edit after comments:

If you do want to keep the default behaviour, but do your logic before everything, you can put your submit handler as first, as they are executed based on their order in the array like this:

array_unshift($form['#submit'], 'module_form_submit');