User email address validation(syntax and unqiue)

Public

Assuming the function as custom validation function for user add form
This function validates the email

Get raw version
php
  1. // Validate the e-mail address, and check if it is taken by an existing user.
  2. function _mymodule_user_validation_function($form, $form_state) {
  3. $email = $form_state['values']['email'];
  4. if ($error = user_validate_mail($mail)) {
  5. form_set_error('email', $error);
  6. return FALSE;
  7. }
  8. else {
  9. $validate_email_flag = _mymodule_user_validate_mail($email);
  10. if ($validate_email_flag == 1) {
  11. form_set_error('email', t('email already exists'));
  12. return FALSE;
  13. }
  14. }
  15. }
  16.  
  17. function _mydmoule_user_validate_mail($email) {
  18. if (db_query("SELECT COUNT(*) FROM {users} WHERE mail = :name", array(':name' => $email))->fetchField()) {
  19. return 1;
  20. } return 2;
  21. }

Comments

Michal's picture

In this state it validates nothing :P

Soliyappan's picture

Here
if ($error = user_validate_mail($mail)) will be
if ($error = user_validate_mail($email))