Problem/Motivation

Currently an email address like xx@xx passes clientside validation but it is rejected by server side validation.

This is due to the fact that the JS validation provided by the jQuery validation library is done on RFC1123 (https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address), which authorizes this possibility.

Whereas Drupal Core validation, done with PHP library egulias/email-validator is against another RFC which does not authorize that.

Proposed resolution

Override jquery validate email validation regex to avoid this possibility.

Remaining tasks

Create MR.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Grimreaper created an issue. See original summary.

grimreaper’s picture

Assigned: grimreaper » Unassigned
Status: Active » Needs review
StatusFileSize
new2.68 KB

Uploading patch for easier composer usage.

Thanks for the review!

j.’s picture

Thank you for the patch. I was having issues with forms submitting with email adress just like that and triggering wierd errors on the server side. This fixed it.

I am using drupal 9.7 and php 7.4.

striknin’s picture

StatusFileSize
new2.09 KB

Hi, thanks for this usefull patch.
But what about email addresses with subdomain, like XXX@sub.domain.ext ?

We need to allow repeated segments with dots, after the @.
So, by changing the regex by the one proposed in this new patch, the validation passes for email addresses with subdomain.

grimreaper’s picture

Assigned: Unassigned » grimreaper
Status: Needs review » Needs work

Hi,

@Striknin, thanks for the patch, this is a case I am encountering on my project too.

But with this patch, xx@xx is now valid again which was the original point of the issue.

I will update the MR.

grimreaper’s picture

Status: Needs work » Needs review

This is regex from Jquery library:

return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );

This is the new proposed one:

return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/.test( value );

I removed the "?" to have a first domain part. And changed the "*" to "+" to have any depth of subdomains. But with at last a first level.

xxx@xxx => FALSE
xxx@xxx.xxx => OK
xxx@xxx.xxx.xxx => OK

etc.

grimreaper’s picture

Assigned: grimreaper » Unassigned
StatusFileSize
new3.84 KB

Uploading patch for Composer usage.

striknin’s picture

Hello,
@Grimreaper you're absolutely right! My bad, I didn't re-check after changing the regex ...
It's now perfect, thank you !

grimreaper’s picture

Status: Needs review » Reviewed & tested by the community

@Striknin, thanks to have re-tested.

Changing to RTBC regarding comment 9.

nigelcunningham’s picture

Would it be feasible to make the change optional? Sometimes, such as when testing in the context of a local development environment, you might validly want to just enter user@localhost.

Regards,

Nigel

nikunjkotecha’s picture

Status: Reviewed & tested by the community » Needs work

I agree, changing the validation should be based on configuration.

sakthi_dev made their first commit to this issue’s fork.

sakthi_dev’s picture

Status: Needs work » Needs review

Made the jquery over ride based on config. Please review.

nikunjkotecha’s picture

Status: Needs review » Needs work

Feedback on MR

dhruv.mittal’s picture

Assigned: Unassigned » dhruv.mittal
dhruv.mittal’s picture

Assigned: dhruv.mittal » Unassigned
Status: Needs work » Needs review

Please review

nikunjkotecha’s picture

Status: Needs review » Needs work

Thanks for the updates Dhruv, can you please check code formatting issues?

grimreaper’s picture

Assigned: Unassigned » grimreaper
grimreaper’s picture

Assigned: grimreaper » Unassigned
Status: Needs work » Needs review