{"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 &#8211; 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&#8217;t 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&#8217;t handle it is just offensive and impolite from the user&#8217;s 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">email address code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">try {<br\/> address = new MailAddress(address).Address; } catch(FormatException<br\/>{ \/\/address is invalid<br\/> } <\/code><\/pre> <\/div>\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&#8217;s 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&#8217;s 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&#8217;s &#8220;case insensitive&#8221; 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=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 5<\/label><\/p>\n<ul>\n<li>Email Regular Expression Pattern<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">email address code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*<br\/>  A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$; <\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">email address code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">^ #start of the line<br\/> [_A-Za-z0-9-\\\\+]+ # must start with string in the bracket [ ], must contains one or more (+)<br\/> (                # start of group #1 <br\/>\\\\.[_A-Za-z0-9-]+ # follow by a dot &quot;.&quot; and string in the bracket [ ], must contains one or more (+)<br\/> )*               # end of group #1, this group is optional (*)<br\/> @                # must contains a &quot;@&quot; symbol<br\/> [A-Za-z0-9-]+ # follow by string in the bracket [ ], must contains one or more (+)<br\/> (              # start of group #2 - first level TLD checking <br\/>\\\\.[A-Za-z0-9]+ # follow by a dot &quot;.&quot; and string in the bracket [ ], must contains one or more<br\/> (+)<br\/> )*            # end of group #2, this group is optional (*)<br\/> (             # start of group #3 - second level TLD checking <br\/>\\\\.[A-Za-z]{2,} # follow by a dot &quot;.&quot; and string in the bracket [ ], with minimum length of 2<br\/> )            # end of group #3 $ <br\/>              #end of the line<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php validate email code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/^\\S+@\\S+\\.\\S+$\/ <\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 7<\/label><\/p>\n<p>Context:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">email address code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">^[a-zA-Z0-9.!#$%&amp;&#039;*+\/=?^_`{|}~-]+@[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])?)*$ <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php validate email code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">^\\w+([-+.&#039;]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$ <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php validate email code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">protected void emailValidator_ServerValidate(object source, ServerValidateEventArgs args) <br\/>{ <br\/>try <br\/>{ <br\/>var a = new MailAddress(txtEmail.Text); <br\/>} <br\/>catch (Exception ex)<br\/>{<br\/>args.IsValid = false;<br\/>emailValidator.ErrorMessage = &quot;email: &quot; + ex.Message; } } <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php validate email code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">([-!#-&#039;*+\/-9=?A-Z^-~]+(\\.[-!#-&#039;*+\/-9=?A-Z^-~]+)*|&quot;([]!#-[^-~ \\t]|(\\\\[\\t -~]))+&quot;)@[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?(\\.[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?)+<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php validate email code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">filter_var($value, FILTER_VALIDATE_EMAIL) <\/code><\/pre> <\/div>\n<ul>\n<li>If we&#8217;re 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 &#8211; 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&#8217;t 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}]}}