I am trying to validate my registration form in a custom module but even 'allowed' chars is returning the error message (Loggitobbogan is also installed). Am I making a rookie mistake in my script, please?

 * Implements hook_form_FORM_ID_alter
function myApp_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['locale']['#access'] = ($form['#user_category'] == 'account' || ($form['#user_category'] == 'register'));

  $form['privacy'] = array(
    '#type' => 'checkbox',
    '#required' => TRUE,
    '#title' => t('Please accept ') . ' ' . l(t('Terms of Use and Privacy'), t('terms-use-and-privacy')),
    '#weight' => 10

  $form['field_full_name']['und'][0]['value']['#attributes']['placeholder'] = t('Chose a user name');
  $form['account']['mail']['#attributes']['placeholder'] = t('Your email address');

  // Change the value of the submit button
  $form['actions']['submit']['#value'] = t('Register');

  //look for illegal chars in field_full_name
  $form['#validate'][] = 'myApp_fullname_registration_form_validate';

//function to look for illegal chars in field_full_name
function myApp_fullname_registration_form_validate(&$form, &$form_state) {
  $fullName = $form_state['values']['field_full_name']['und'][0];
  if (!preg_match( '/[^A-Z|a-z|0-9]*/', $fullName )) {
    form_set_error('field_full_name', t('Illegal chars used?!'));

Any guidance, advice, pointers are gratefully accepted! Thank you.

This is the whole relevant section from myApp_registration.module file, where I can change placeholder text successfully, and where I believe I could add my form_validation script. It is cut and pasted and works fine, except the new form_validate function.

It is Drupal 7.

  • Please post the code your actually using, or the correct error message. The code in the question has syntax errors and would not return a form error like you suggest
    – Clive
    Sep 3 at 13:22
  • The error message that I paste as a string in form_set_error comes through on the front end nicely, except that it returns for whatever value I enter in the field, valid or invalid chars - thanks for looking. Sep 3 at 13:47
  • The code changed since my last comment - I would check your regular expression it looks very greedy
    – Clive
    Sep 3 at 14:06
  • Ah yes, you asked me to post my actual code, so it changed slightly. I thought that regular expression would only allow a-z, A-Z and 0-9 but I'll take another look, they are trixy those regexes. That said, "if (!ctype_alnum ..." does the same thing, returns the error for either value. Sep 3 at 14:15
  • 1
    I can't understand your last comment. If what does what, the same thing happens? I don't see any ctype_al... function in your code. Please make sure you update the code in your post to match your current comments, so we can follow what you are asking.
    – Jaypan
    Sep 4 at 15:57

Your Answer

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

Browse other questions tagged or ask your own question.