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.