Validation programmatically in Drupal
Validation is pretty much depends on what kind of fields are they! Still I can show validation for the common fields as on user registration process.

You must be hooking into the user registration form somewhere, unless form is custom form.Assuming you are implementing hook_form_alter() to hook into user registration form.

&gt; Step 1: Add validation handler to the user registration form.

    // Implementing hook_form_alter() to add validation handler to user registration form.
    function mymodulename_form_alter($form,$form_state,$form_id) {

    // Not sure you have to check form id of the user registration form.
      if($form_id == "user_registration") {
        $form['#validate'] = array(
          '#validate' =&gt; array('_mymodulename_registration_validate'),
        );
      }
    }

    // Write validation callback.
    function _mymodulename_registration_validate(&amp;$form,&amp;$form_state) {
       // Validate last name.
      if (trim($form_state['values']['last_name']) == '') {
        form_set_error('last_name', 'Last Name field is required field');
      }

      // Validate company name.
      if (trim($form_state['values']['company_name']) == '') {
        form_set_error('company_name', 'Company Name field is required field');
      }

      // Validate the e-mail address.
      if (!valid_email_address(trim($form_state['values']['email']))) {
        form_set_error('email', 'Please enter valid email address');
      }
      if (_mymodulename_registration_user_validate_mail(trim($form_state['values']['email'])) == 1) {
        form_set_error('email', 'Email address already registered');
      } 
        // Validate password.
      if (trim($form_state['values']['password']) == '') {
        form_set_error('password', 'Password field is required');
      }
      if (trim($form_state['values']['password_confirm']) == '') {
        form_set_error('password_confirm', 'Repeat password field is required');
      }
      if (trim($form_state['values']['password_confirm']) != trim($form_state['values']['password'])) {
        form_set_error('password_confirm', 'Passwords do not match');
      }
    }

/**
 * Helper function to validate email adress already exists or not.
 *
 */
function _mymodulename_registration_user_validate_mail($email) {

  // used db_query_range() to check if email exists or not.
  if ((bool) db_query_range("SELECT mail FROM {users} WHERE mail = :name", 0, 1, array(':name' =&gt; $email))-&gt;fetchField()) {
    return 1;
  } return 2;
}