Javascript email authentication

Is this the correct javascript (checkValidity) function?

function checkTextBox(textBox) { if (!checkValidity(textBox.getValue())) displayError("Error title", "Error message", textBox); textBox.focus(); } function checkValidity(e) { var email; email = "/^[^@] +@ [^@]+.[az]{2,}$/i"; if (!e.match(email)){ return false; else return true; } } 

EDIT: All answers are appreciated! Thanks!

+4
source share
8 answers

The email address is defined in RFC 5322, Β§ 3.4 . The corresponding non terminal is addr-spec . This definition, apparently, is somewhat modest in nature, both due to complications of the domain specifications and to support old, obsolete forms. However, you can do an override for most forms:

 ^[-0-9A-Za-z!#$%&'*+/=?^_`{|}~.] +@ [-0-9A-Za-z!#$%&'*+/=?^_`{|}~.]+ 

Please note that there are a very large number of legal symbols. Most registries are mistakenly listed. Yes, all of these characters are legal by email.

This regular expression will not follow some very common forms, for example, "noodle soup @ 9"@[what the.example.com] is the legal email address!

+7
source
 function isValidEmail($email) { return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[az]{2,3})$", $email); }; if(isValidEmail(" name@yahoo.com ")) { echo "valid"; } else { echo "aaa"; }; 
+3
source

No, this regular expression is not suitable for this purpose. Look at this instead (although I cannot guarantee its authenticity).
Also, as for the script itself, why don't you test it like this:

 function checkEmailValidity(e) { return e.match("some validating regex"); } 

This seems like a faster, more understandable, and more readable solution.

EDIT:
It is worth noting that it is almost impossible to write a regular expression that any valid email address can detect. Therefore, you might be better off trying to do some validation algorithm rather than a regular expression, since valid email addresses can be very, very complex.

Consider this code:

 function validateEmail(email) { if (typeof email != "string") return false; email = email.split("@"); email[1] = email[1].split("."); if ( email.length !== 2 || email[1].length < 2 || !email[1].hasValues(String) ) return false; return true; } // Checks whether each key in an array has a value, and (optionally) if they match a given contructor (object type). // Ie: myArray.hasValues(String) // checks whether all elements in myArray has a value, a nonempty string. Array.prototype.hasValues = function(assumedConstructor) { for (var i = 0; i < this.length; i++) { if (!this[i]) return false; if (assumedConstructor && this[i].constructor != assumedConstructor) return false; } return true; }; 

It works as follows:

  • First check if the string contains one @ and only one
  • Checks that the part after @ has at least one .
  • Checks if there are any characters between all possible . .

It will still be easy to fake a fake email address, but in this way we guarantee that it is at least somehow formatted. The only thing I can think of is @ internal comments, which should be completely legal according to the RFC, but this code will treat it as an error.
Ordinary internet users with regular email addresses will not let you down. So, how important this is, it's up to you to decide;)

The best solution, if available, is to put the address in some kind of built-in method that somehow validates by trying to use the email address.

+2
source

Not. It is assumed that an email address can contain only one @ . I would suggest reading this article .

You probably also meant \. instead of . .

+1
source

Try the following: I'm sure it takes care of all kinds of email checking.

 function checkEmail(email) { if(/^([az]([az]|\d|\+|-|\.)*):(\/\/(((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([az]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([az]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(email)) { return true; } else { return false; } } 

NTN

+1
source

Here's a regex matching RFC, also excluding legacy forms. I broke it down into components so that it is easy to read:

 IDENT = [a-z0-9](?:[a-z0-9-]*[a-z0-9])? TEXT = [a-z0-9!#$%&'*+/=?^_`{|}~-]+ EMAIL = TEXT(?:\.TEXT)*@IDENT(?:\.IDENT)+ 

(Note: case insensitive.)

This will not match the email addresses that use the quoted form before the @ form or the parenthesized form after it, but as long as these forms are valid, they are hardly ever found outside of the examples at present, and they greatly complicate the regular expression.

+1
source

Verifying your email address is very complicated. It is not even worth checking on the client side, except for a simple check for the @ and characters . .

Section 3.4.1 of RFC 5322 describes a huge number of legal characters, and you will see that creating bulletproof regex will be almost impossible.

I ended up refusing verification because I will have random complaints from users saying their crazy email address is not working. So now I'm just trying to send an email and hope it arrives. If it does not send, I inform the user that his email address is problematic.

+1
source

Performing a couple of searches, I came across this. Its based on RFC 2822

http://snipplr.com/view/20981/

0
source

Source: https://habr.com/ru/post/1309045/


All Articles