New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed Verifiy email using regex and modified Login action #236
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbf1af0
Merge pull request #232 from FanrayMedia/dev
3fb3b5f
Fixed Verifiy email using regex
flyznex 3e2d3ca
added RegexUtilitiesTest.cs
flyznex 8b31882
remove comments
flyznex cd8a303
fix: Change RegexUtilites to an instance class #234
834985d
remove _invalid variable
flyznex 9f9c82e
remove check ArgumentNullException
flyznex f910ce9
fix: Consolidate email valid feature into existing Util class #234
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Fan.Helpers | ||
{ | ||
/// <summary> | ||
/// A util class that uses regular expression to verify if a string is in valid email format. | ||
/// </summary> | ||
/// <remarks> | ||
/// https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format | ||
/// </remarks> | ||
public static class RegexUtilities | ||
{ | ||
public static bool IsValidEmail(string strIn) | ||
{ | ||
if (String.IsNullOrEmpty(strIn)) | ||
return false; | ||
|
||
// Use IdnMapping class to convert Unicode domain names. | ||
try | ||
{ | ||
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper, | ||
RegexOptions.None, TimeSpan.FromMilliseconds(200)); | ||
if(string.IsNullOrEmpty(strIn)) | ||
return false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
if(ex is RegexMatchTimeoutException || ex is ArgumentNullException) | ||
return false; | ||
} | ||
// Return true if strIn is in valid email format. | ||
try | ||
{ | ||
return Regex.IsMatch(strIn, | ||
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + | ||
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$", | ||
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)); | ||
} | ||
catch (RegexMatchTimeoutException) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
private static string DomainMapper(Match match) | ||
{ | ||
// IdnMapping class with default property values. | ||
IdnMapping idn = new IdnMapping(); | ||
|
||
string domainName = match.Groups[2].Value; | ||
try | ||
{ | ||
domainName = idn.GetAscii(domainName); | ||
} | ||
catch (ArgumentException) | ||
{ | ||
return null; | ||
} | ||
return match.Groups[1].Value + domainName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Fan.Helpers; | ||
using Xunit; | ||
|
||
namespace Fan.UnitTests.Helpers | ||
{ | ||
/// <summary> | ||
/// Test for <see cref="RegexUtilities"/> class. | ||
/// </summary> | ||
public class RegexUtilitiesTest | ||
{ | ||
/// <summary> | ||
/// Test cases for <see cref="RegexUtilities.IsValidEmail(string)"/> method. | ||
/// </summary> | ||
/// <param name="email"></param> | ||
/// <param name="expected"></param> | ||
/// <remarks> | ||
/// Test data provided by https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format#compiling-the-code | ||
/// </remarks> | ||
[Theory] | ||
[InlineData("david.jones@proseware.com", true)] | ||
[InlineData("d.j@server1.proseware.com", true)] | ||
[InlineData("jones@ms1.proseware.com", true)] | ||
[InlineData("j.@server1.proseware.com", false)] | ||
[InlineData("j@proseware.com9", true)] | ||
[InlineData("js#internal@proseware.com", true)] | ||
[InlineData("j_9@[129.126.118.1]", true)] | ||
[InlineData("j..s@proseware.com", false)] | ||
[InlineData("js*@proseware.com", false)] | ||
[InlineData("js@proseware..com", false)] | ||
[InlineData("js@proseware.com9", true)] | ||
[InlineData("j.s@server1.proseware.com", true)] | ||
[InlineData(@"""j\""s\""""@proseware.com", true)] | ||
[InlineData("js@contoso.中国", true)] | ||
[InlineData("username", false)] | ||
public void IsValidEmail_Test(string email, bool expected) | ||
{ | ||
Assert.Equal(expected, RegexUtilities.IsValidEmail(email)); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are checking for null on line 25, why would you check for
ArgumentNullException
on this line?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I forgot to remove it. My mistake!