How can I validate an email address in JavaScript?

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium) const validateEmail = (email) => { return String(email) .toLowerCase() .match( /^(([^<>()[\]\\.,;:\s@”]+(\.[^<>()[\]\\.,;:\s@”]+)*)|(“.+”))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); }; Here’s the example of a regular expression that accepts unicode: const re = /^(([^<>()[\]\.,;:\s@\”]+(\.[^<>()[\]\.,;:\s@\”]+)*)|(\”.+\”))@(([^<>()[\]\.,;:\s@\”]+\.)+[^<>()[\]\.,;:\s@\”]{2,})$/i; But keep in mind that one should not rely … Read more

Validating email addresses using jQuery and regex

UPDATES http://so.lucafilosofi.com/jquery-validate-e-mail-address-regex/ using new regex added support for Address tags (+ sign) function isValidEmailAddress(emailAddress) { var pattern = /^([a-z\d!#$%&’*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&’*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|”((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?”)@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i; return pattern.test(emailAddress); } if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ } NOTE: keep in mind that no 100% regex email check exists!

C# code to validate email address

What about this? bool IsValidEmail(string email) { var trimmedEmail = email.Trim(); if (trimmedEmail.EndsWith(“.”)) { return false; // suggested by @TK-421 } try { var addr = new System.Net.Mail.MailAddress(email); return addr.Address == trimmedEmail; } catch { return false; } } Per Stuart’s comment, this compares the final address with the original string instead of always returning … Read more

How should I validate an e-mail address?

Another option is the built in Patterns starting with API Level 8: public final static boolean isValidEmail(CharSequence target) { if (TextUtils.isEmpty(target)) { return false; } else { return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } } Patterns viewable source OR One line solution from @AdamvandenHoven: public final static boolean isValidEmail(CharSequence target) { return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); }

How to validate an Email in PHP?

You can use the filter_var() function, which gives you a lot of handy validation and sanitization options. filter_var($email, FILTER_VALIDATE_EMAIL) PHP Manual filter_var() Available in PHP >= 5.2.0 If you don’t want to change your code that relied on your function, just do: function isValidEmail($email){ return filter_var($email, FILTER_VALIDATE_EMAIL) !== false; } Note: For other uses (where … Read more

What characters are allowed in an email address?

See RFC 5322: Internet Message Format and, to a lesser extent, RFC 5321: Simple Mail Transfer Protocol. RFC 822 also covers email addresses, but it deals mostly with its structure: addr-spec = local-part “@” domain ; global address local-part = word *(“.” word) ; uninterpreted ; case-preserved domain = sub-domain *(“.” sub-domain) sub-domain = domain-ref … Read more

How to validate an email address in PHP

The easiest and safest way to check whether an email address is well-formed is to use the filter_var() function: if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // invalid emailaddress } Additionally you can check whether the domain defines an MX record: if (!checkdnsrr($domain, ‘MX’)) { // domain is not valid } But this still doesn’t guarantee that the … Read more