S
S
Sergey Konovalenko2021-05-05 21:50:57
JavaScript
Sergey Konovalenko, 2021-05-05 21:50:57

How to tweak email regex in jQuery Validation Plugin?

I use the jQuery Validation Plugin for form validation .

Its built-in method for validating email also considers mailboxes that have a simple top-level domain specified, for example [email protected]

, as valid. But the customer wants to make such mailboxes considered invalid.

Tell me how to change this regular expression so that it is necessary to specify a dot in the hostname and after which there must be some top-level domain?

The plugin uses the same regular expression that the HTML5 specification suggests to browsers .

// Sets a custom email pattern for the built-in email validation rule.
// https://jqueryvalidation.org/jQuery.validator.methods/
$.validator.methods.email = function( value, element ) {
    return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-][email protected][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 );
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question