0

I want to add a special character validation on a specfic field. Here I have my code but it does not work and there is no error.

function mymodule_form_alter(&$form, &$form_state, &$form_id) { 
   if ($form_id == 'registration_node_form') {
        $form['#validate'][] = 'mymodule_form_validate';        
   }
}

function mymodule_form_validate($form, $form_state){
    if(isset ($form['field_employer_name']['und'][0]['value']['#default_value'])){
        $special_character_list = '[\'^£$%&*()}{@#~?><>,|=_+¬-]'; 
        if (preg_match($special_character_list, $form_state['field_employer_name']['und'][0]['value']['#default_value'])== 0) {
            form_set_error('field_employer_name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters except dash (-) and underscore (_) are not allowed.'));
        }
    }
}

Please help. Thank You.

2

Try using Field Validation Module it simplifies your work!!

If you want to code it then check this post It solves the same issue

Try changing your code:

 $special_character_list = '\'^£$%&*()}{@#~?><>,|=_+¬\-\[\]';
  if(preg_match('/[' . $special_character_list . ']/',$form_state['field_employer_name']['und'][0]['value']['#default_value']) !== 0)
  • already use but not achieve – Adi Nov 11 '14 at 11:47
  • Did you checked the link post I shared – Pravin Ajaaz Nov 11 '14 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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