Use valid_email_address() to validate email. was: Unable to do email verification
bartman76 - January 16, 2009 - 12:17
| Project: | Author Contact |
| Version: | 6.x-1.1 |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
Hi I install and everything is working fine, however when I input an invalid email it failed to validate, is there any way to trigger this?
Thanks

#1
What email are you trying to validate (or invalidate)?
Keep in mind that it only validates to make sure the email is a possible email address by seeing if it looks like an emailey kind of string - it can't check if it's an actual factual real live email address.
If you let me know the addresses/strings you want to fail I'll test it and see what we can do.
#2
Thanks for a great module.
I have the same problem: I can enter anything, "rhjdkrghkd gdr" for example, in the email field, and it is accepted. It seems like the validation isn't being run at all...
Thanks
#3
This may be related to authorcontact not using the drupal core validation function and doing verification itself :)
I think it should be using valid_email_address() to validate addresses, not that regex, so current code:
function authorcontact_form_validate($form_id, $form_values) {//check the email address is valid
if(isset($form_values['email'])) {
if (!preg_match('/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/', $form_values['email'])) {
form_set_error('email', t('Invalid email address'));
}
}
}
should be*:
function authorcontact_form_validate($form_id, $form_values) {//check the email address is valid
if(isset($form_values['email'])) {
if (!valid_email_address($form_values['email'])) {
form_set_error('mail', t('The e-mail address you specified is not valid.'));
}
}
}
* partially cribbed from comment module, you may want to make more changes :)
Function documentation:
http://api.drupal.org/api/function/valid_email_address/6
- changed issue title to reflect this
#4
Actually there's more to this issue than that, it doesn't actually validate the email address at all (as everyone says). You can enter anything. Will look into this further tomorrow if no one else does before then :)
#5
Thanks Adrinux, that's a hot tip - I was simply doing it myself because I wasn't aware of valid_email_address().
I'll work that in asap. (While fixing CVS...)
#6
#7
Thank you for the patch, nice job!
The validation now works, but I had to add a comma to line 56:
'#required' => true,I have added the comma to the patch and reuploaded it, I hope correctly?
I haven't applied a patch before, but it's not that difficult. If anybody else want to test the patch, just follow the directions here http://drupal.org/patch/apply.
#8
Good catch, but no, you can't simply edit the patch, as it stands patch would entirely ignore your change. you need to diff compared to the original again.
Corrected patch attached.
#9
Thanks Adrinux for the patch and ressa for the testing. I've updated the 6.x dev version with this validation method.
Thanks again!
#10
Automatically closed -- issue fixed for 2 weeks with no activity.