I have been setting up several categories of profile fields for users
and would like to validate some of the fields input.
For instance, for a YouTube url, I want to validate that it is, in fact YouTube and not SpamTube.

Is there any way to achieve regex validation of profile fields?
Anyone know the hook, ( profile_validate_profile ) ?

Of course, this would have to hook the profile form and the registration form.
( _user_register ) ?

Thanks

Comments

halloffame’s picture

Found any solution yet?

xDudditzx’s picture

I did find a module approach to validation which I found here:
http://thedrupalblog.com/adding-validation-user-edit-form-ensure-usernam...

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  // test for the user profile edit form
  if ($form_id == 'user_profile_form') {
    // add another validation function to the list
    $form['#validate'][] = '_MYMODULE_form_alter_user_profile_validate';
  }
}

// define the validation function
function _MYMODULE_form_alter_user_profile_validate($form, &$form_state) {
  // validate the username and set a form error as necessary
  if (!valid_email_address($form_state['values']['name']))
    form_set_error('name', 'Username must be a valid email address.');
}
?>

If you wanted to also validate on registration, you would alter the conditional like so:
if ($form_id == 'user_register' || $form_id == 'user_profile_form') {

I did not need email validation but rather a string length check so my validation looked similar to this:

  // validate the MYFIELD and set a form error as necessary
  $MYFIELD = $form_state['values']['MYFIELD'];
  if (strlen($MYFIELD) >= 16)
    form_set_error('MYFIELD', 'MY ERROR MESSAGE.');

Still hope to find a better way aside from using a custom module.

xDudditzx’s picture

Thank you.
I have no clue what I was working on at that time, but I thank you anyway for the help.