{"id":212,"date":"2017-03-14T08:22:14","date_gmt":"2017-03-14T08:22:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=212"},"modified":"2017-03-29T18:28:42","modified_gmt":"2017-03-29T12:58:42","slug":"using-a-regular-expression-to-validate-an-email-address","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/using-a-regular-expression-to-validate-an-email-address\/","title":{"rendered":"[Solved &#8211; 11 Answers] Using a regular expression to validate an email address"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM<\/label><\/p>\n<p>regex for email validation \u2013 There is a several PHP programs. Get contacted by someone that is having trouble with a site that uses it, and we end up having to make some adjustment (most recently we realized that wasn\u2019t allowing 4-character TLDs).<\/p>\n<p><label class=\"label label-info\">SOLUTION 1<\/label><\/p>\n<p>For PHP, we should\u00a0not\u00a0use the pattern (regex for email validation) given in\u00a0Validate an E-Mail Address with PHP, the Right Way.<\/p>\n<ul>\n<li>That is no better than all the other non-RFC patterns. It isn\u2019t even smart enough to handle even\u00a0RFC 822, let alone RFC 5322.<\/li>\n<li>If we need to get visualize and sophistic,\u00a0implement a complete state engine. A regular expression can only act as a basic filter. The problem with regular expressions is that telling someone that their perfectly valid e-mail address is invalid (a false positive) because our regular expression can\u2019t handle it is just offensive and impolite from the user\u2019s perspective.<\/li>\n<li>A state engine for the purpose can both validate and even correct e-mail addresses that would otherwise be considered invalid as it disassembles the e-mail address according to each RFC.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION\u00a02<\/label><\/p>\n<ul>\n<li>We should not use regular expressions to validate email addresses.<\/li>\n<li>Instead, use the\u00a0MailAddress\u00a0class, like this:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dtry%20%7B%0A%20address%20%3D%20new%20MailAddress(address).Address%3B%20%7D%20catch(FormatException%0A%7B%20%2F%2Faddress%20is%20invalid%0A%20%7D%20%0A\u201d message=\u201demail address code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>The MailAddress class uses a BNF parser to validate the address in full accordance with RFC822.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 3<\/label><\/p>\n<p>why\u00a0we need to validate email addresses? What is the real benefit for validating?<\/p>\n<ul>\n<li>It will not catch common typos.<\/li>\n<li>It does not prevent people from entering invalid or made-up email addresses, or entering someone else\u2019s address.<\/li>\n<\/ul>\n<p>If we need to validate that an email is correct, we have no choice than to send an confirmation email and have the user reply to that. In many cases we have to send a confirmation mail anyway for security reasons or for ethical reasons<\/p>\n<p><label class=\"label label-info\">SOLUTION 4<\/label><\/p>\n<ul>\n<li>If we need to use the regular expression, there\u2019s two things we need to understand.<\/li>\n<li>First, long regex for email validation make it difficult to nicely format paragraphs. So we don\u2019t include\u00a0a-z\u00a0in any of the three character classes. This regex is intended to be used with our regex engine\u2019s \u201ccase insensitive\u201d option turned on.<\/li>\n<li>Second, the above regex is delimited with\u00a0word boundaries, which makes it suitable for extracting email addresses from files or larger blocks of text. If we need to check whether the user typed in a valid email address, replace the word boundaries with\u00a0start-of-string and end-of-string anchors, like this:\u00a0^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 5<\/label><\/p>\n<ul>\n<li>Email Regular Expression Pattern<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%5E%5B_A-Za-z0-9-%5C%5C%2B%5D%2B(%5C%5C.%5B_A-Za-z0-9-%5D%2B)*%0A%20%20A-Za-z0-9-%5D%2B(%5C%5C.%5BA-Za-z0-9%5D%2B)*(%5C%5C.%5BA-Za-z%5D%7B2%2C%7D)%24%3B%20%0A\u201d message=\u201demail address code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dphp\u201d manual=\u201d%5E%20%23start%20of%20the%20line%0A%20%5B_A-Za-z0-9-%5C%5C%2B%5D%2B%20%23%20must%20start%20with%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%231%20%0A%5C%5C.%5B_A-Za-z0-9-%5D%2B%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20)*%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%231%2C%20this%20group%20is%20optional%20(*)%0A%20%40%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20must%20contains%20a%20%22%40%22%20symbol%0A%20%5BA-Za-z0-9-%5D%2B%20%23%20follow%20by%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%20(%2B)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%232%20-%20first%20level%20TLD%20checking%20%0A%5C%5C.%5BA-Za-z0-9%5D%2B%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20must%20contains%20one%20or%20more%0A%20(%2B)%0A%20)*%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%232%2C%20this%20group%20is%20optional%20(*)%0A%20(%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20start%20of%20group%20%233%20-%20second%20level%20TLD%20checking%20%0A%5C%5C.%5BA-Za-z%5D%7B2%2C%7D%20%23%20follow%20by%20a%20dot%20%22.%22%20and%20string%20in%20the%20bracket%20%5B%20%5D%2C%20with%20minimum%20length%20of%202%0A%20)%20%20%20%20%20%20%20%20%20%20%20%20%23%20end%20of%20group%20%233%20%24%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23end%20of%20the%20line%0A\u201d message=\u201demail address code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6<\/label><\/p>\n<p>Examples:<br \/>\nvenkat@ gmail.com\u00a0(spaces in emails) or\u00a0lavanya\u00a0(no domain at all) or\u00a0lavs@gmailcom\u00a0(no period before .com),<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F%5E%5CS%2B%40%5CS%2B%5C.%5CS%2B%24%2F%20%0A\u201d message=\u201dphp validate email code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 7<\/label><\/p>\n<p>Context:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%5E%5Ba-zA-Z0-9.!%23%24%25%26\u2019*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B%40%5Ba-zA-Z0-9%5D(%3F%3A%5Ba-zA-Z0-9-%5D%7B0%2C61%7D%5Ba-zA-Z0-9%5D)%3F(%3F%3A%5C.%5Ba-zA-Z0-9%5D(%3F%3A%5Ba-zA-Z0-9-%5D%7B0%2C61%7D%5Ba-zA-Z0-9%5D)%3F)*%24%20%0A\u201d message=\u201demail address code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Note: E-mail addresses that is simultaneously too strict (before the \u201c@\u201d character), too unclear (after the \u201c@\u201d character), and too negligent (allowing comments, whitespace characters, and quoted strings in manners unfamiliar to most users).<\/p>\n<p><label class=\"label label-info\">SOLUTION 8<\/label><\/p>\n<p>The one which is used in ASP.NET by the RegularExpressionValidator.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%5E%5Cw%2B(%5B-%2B.\u2019%5D%5Cw%2B)*%40%5Cw%2B(%5B-.%5D%5Cw%2B)*%5C.%5Cw%2B(%5B-.%5D%5Cw%2B)*%24%20%0A\u201d message=\u201dphp validate email code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9<\/label><\/p>\n<p>The email addresses validation using ASP.NET web application using the System.Net.Mail namespace to send emails to a list of people. So, rather than using some very complex regular expression, Simply try to create a MailAddress instance from the address. The MailAddress constructor will throw an exception if the address is not formed properly.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dprotected%20void%20emailValidator_ServerValidate(object%20source%2C%20ServerValidateEventArgs%20args)%20%0A%7B%20%0Atry%20%0A%7B%20%0Avar%20a%20%3D%20new%20MailAddress(txtEmail.Text)%3B%20%0A%7D%20%0Acatch%20(Exception%20ex)%0A%7B%0Aargs.IsValid%20%3D%20false%3B%0AemailValidator.ErrorMessage%20%3D%20%22email%3A%20%22%20%2B%20ex.Message%3B%20%7D%20%7D%20%0A\u201d message=\u201dphp validate email code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 10<\/label><\/p>\n<ul>\n<li>Use the following regex for email validation for input validation:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d(%5B-!%23-\u2018*%2B%2F-9%3D%3FA-Z%5E-~%5D%2B(%5C.%5B-!%23-\u2018*%2B%2F-9%3D%3FA-Z%5E-~%5D%2B)*%7C%22(%5B%5D!%23-%5B%5E-~%20%5Ct%5D%7C(%5C%5C%5B%5Ct%20-~%5D))%2B%22)%40%5B0-9A-Za-z%5D(%5B0-9A-Za-z-%5D%7B0%2C61%7D%5B0-9A-Za-z%5D)%3F(%5C.%5B0-9A-Za-z%5D(%5B0-9A-Za-z-%5D%7B0%2C61%7D%5B0-9A-Za-z%5D)%3F)%2B\u201d message=\u201dphp validate email code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Addresses matched by this regex for email validation:<\/p>\n<ul>\n<li>Have a local part (i.e. the part before the @-sign) that is strictly compliant with RFC 5321\/5322,<\/li>\n<li>Have a domain part (i.e. the part after the @-sign) that is a host name with at least two labels, each of which is at most 63 characters long.<\/li>\n<\/ul>\n<p>The second constraint is a restriction on RFC 5321\/5322<\/p>\n<p><label class=\"label label-info\">SOLUTION 11<\/label><\/p>\n<ul>\n<li>Use the PHP build-in validation for emails.<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dfilter_var(%24value%2C%20FILTER_VALIDATE_EMAIL)%20%0A\u201d message=\u201dphp validate email code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If we\u2019re running a php-version lower than 5.3.6 please be aware of this issue:\u00a0https:\/\/bugs.php.net\/bug.php?id=53091<\/li>\n<\/ul>\n<ul>\n<li>This article provides some of the basic informations on\u00a0email address , validate email , php validate email , php email validation , email tester , email validation javascript , email validation php , php email , php form validation , emailvalidator , php validator , validation for email , form validation php , email validation regex , form validation in php , validation in php , email validation in php , regular expression for email , how to validate email in javascript , php contact form with validation , regular expression in php , email verification in php.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM regex for email validation \u2013 There is a several PHP programs. Get contacted by someone that is having trouble with a site that uses it, and we end up having to make some adjustment (most recently we realized that wasn\u2019t allowing 4-character TLDs). SOLUTION 1 For PHP, we should\u00a0not\u00a0use the pattern (regex for email [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[488,3569,485,479,481,482,491,3572,3584,3573,3574,3581,476,3589,480,3577,494,3582,3580,483,3586,495,486,3587,3575,489,3576,3571,3578,490,3585,477,475,493,484,3588,478,492,487,3570,3579,3583],"class_list":["post-212","post","type-post","status-publish","format-standard","hentry","category-php","tag-best-regular-expression-for-email-validation-in-c","tag-email-address","tag-email-address-validation-in-jquery-using-regular-expressions","tag-email-regex-php","tag-email-regex-python","tag-email-regex-ruby","tag-email-regular-expression","tag-email-tester","tag-email-validation-in-php","tag-email-validation-javascript","tag-email-validation-php","tag-email-validation-regex","tag-email-validation-regex-javascript","tag-email-verification-in-php","tag-email-writing-pattern","tag-emailvalidator","tag-find-emails-in-a-string","tag-form-validation-in-php","tag-form-validation-php","tag-how-to-validate-email-address-in-sql","tag-how-to-validate-email-in-javascript","tag-java-code-for-regex-of-email-validation","tag-javascript-regular-expression-email-validation","tag-php-contact-form-with-validation","tag-php-email","tag-php-email-validation","tag-php-form-validation","tag-php-validate-email","tag-php-validator","tag-regex-for-email-validation","tag-regular-expression-for-email","tag-regular-expression-for-email-in-c","tag-regular-expression-for-email-in-java","tag-regular-expression-for-valid-email-address","tag-regular-expression-for-validating-email-address-in-sql-server","tag-regular-expression-in-php","tag-simple-email-regex","tag-using-regex-for-simple-email-validation","tag-valid-email-address-regular-expression","tag-validate-email","tag-validation-for-email","tag-validation-in-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}