Validation programmatically in Drupal I am creating a user programatically. 

I have following files 

$fields = array(

    'name' =&gt; $name,
    'mail' =&gt; $mail,
    'pass' =&gt; $password,
    'status' =&gt; 1,
    'init' =&gt; $mail,
    'field_first_name' =&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$first_name))),
    'field_last_name' =&gt; array('und'=&gt;array(0=&gt;array('value'=&gt;$last_name))),
    'field_company_name'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$company_name))),
    'field_company_number'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$company_number))),
    'field_mobile_number'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$mobile_number))),
    'field_secondary_number'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$secondary_number))),
    'field_mobile_or_landline'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$landlinemobile))),
    'field_promo_emails_notifications'=&gt;array('und'=&gt;array(0=&gt;array('value'=&gt;$email_notification))),


);

Now i am using user_save(NULL,$fields);

Which is working fine. Now field_first_name, and some other fields are required which i have set under account mangefields. How to check validations of these fields. Does anybody tell me 

Thanks
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;
}