Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
function form_example_form($form, &$form_state) {

$form['empcode'] = array(
  '#weight' => '0',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Code'),
);
$form['empname'] = array(
  '#weight' => '1',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Name'),
);
$form['empemail'] = array(
  '#weight' => '2',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Employee Email'),
);
$form['empdesignation'] = array(
  '#weight' => '3',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Designation'),
);
$form['mobile'] = array(
  '#weight' => '4',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Mobile No.'),
);
$form['ipphone'] = array(
  '#weight' => '5',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('IP Phone'),
);
$form['qualifications'] = array(
  '#weight' => '6',
  '#required' => '0',
  '#type' => 'textfield',
  '#title' => t('Qualifications'),
);
$form['profile'] = array(
  '#weight' => '7',
  '#required' => '0',
  '#type' => 'textarea',
  '#title' => t('Profile'),
);
$form['file'] = array(
  '#weight' => '8',
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
);
$form['captcha'] = array(
  '#weight' => '9',
  '#type' => 'captcha',
  '#captcha_type' => 'captcha/Math',
);
 $form['submit'] = array(
     '#weight' => '10',
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

return $form;

}


function form_example_form_validate($form, &$form_state) {
  /*if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }*/
 // print('<pre>'.print_r($form_state['values'],1).'</pre>');
  //die();
 $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

i want to add advance level validations so that no one enter special characters to these form fields. Currently required is working.

share|improve this question

You can use Form API Validation module for this.

It has Available Validators like "alpha_numeric" which accept only Alpha Numeric characters and for more see below image.

enter image description here

share|improve this answer

You can use PHP function(preg_match) like below in form_validate function for Name etc

if (!preg_match('/^[A-Za-z]+$/', $form['submitted']['name']['#value'])) 
   {
     form_set_error('name', t('Please enter alphabets only for Name.'));
   }
share|improve this answer

You can use both client side and server side validation.

I am showing an example of server side validation for email.

function form_example_form_validate($form, &$form_state) {
  //for email
    $email = $form_state['values']['empemail'];
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            form_set_error('email', t('Invalid email id'));
        }
 //for number
   $mobile = $form_state['values']['mobile'];
   if (!is_numeric($mobile)) {
       form_set_error('mobile', t('Not a number'));
   }
 // for alphanumeric
 $empcode = $form_state['values']['empcode'];
 if (!ctype_alnum($empcode)) {
    form_set_error('empcode', t('Not alphanumeric'));
}  
}

Or

You can install Elements module.

This module intends to become a repository for advanced form elements.

Support HTML5 elements (url, email, search, tel, number, range)

Or

Can try the Field Validation module also.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.