Last active
April 27, 2023 17:42
-
-
Save kleinron/fed76198cd4661cc4b2a488ee891de99 to your computer and use it in GitHub Desktop.
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
Pattern phoneNumber = Pattern.With | |
.AtBeginning.Choice.Either( | |
Pattern.With.Literal("02"), | |
Pattern.With.Literal("03"), | |
Pattern.With.Literal("04"), | |
Pattern.With.Literal("08"), | |
Pattern.With.Literal("09"), | |
Pattern.With.Literal("072"), | |
Pattern.With.Literal("073"), | |
Pattern.With.Literal("074"), | |
Pattern.With.Literal("076"), | |
Pattern.With.Literal("077"), | |
Pattern.With.Literal("050"), | |
Pattern.With.Literal("052"), | |
Pattern.With.Literal("054") | |
) | |
.Digit.Repeat.InRange(6,7).AtEnd; |
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
Pattern phoneNumber = Pattern.With | |
.AtBeginning.Choice.Either( | |
Pattern.With.Literal("02"), | |
Pattern.With.Literal("03"), | |
Pattern.With.Literal("04"), | |
Pattern.With.Literal("08"), | |
Pattern.With.Literal("09"), | |
Pattern.With.Literal("072"), | |
Pattern.With.Literal("073"), | |
Pattern.With.Literal("074"), | |
Pattern.With.Literal("076"), | |
Pattern.With.Literal("077"), | |
Pattern.With.Literal("050"), | |
Pattern.With.Literal("052"), | |
Pattern.With.Literal("054") | |
) | |
.WhiteSpace.Repeat.Optional | |
.Literal("-").Repeat.Optional | |
.WhiteSpace.Repeat.Optional | |
.Digit.Repeat.InRange(6,7).AtEnd; |
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
^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$ |
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
03 6382020 |
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
^(([0]([2|3|4|8|9|72|73|74|76|77])))[2-9]\d{6,7}$ |
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
When you work with SharpKit, you write C# instead of JavaScript |
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
Some people, when confronted with a problem, think "I know, I'll use regular expressions." | |
Now they have two problems. |
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
namespace Electron.Validation | |
{ | |
public static class CharExt | |
{ | |
public static bool IsDigit(this char c) | |
{ | |
return c >= '0' && c <= '9'; | |
} | |
} | |
public class PhoneNumber | |
{ | |
public static bool IsValid(string s) | |
{ | |
if (null == s) | |
return false; | |
var validPrefixes = new[] { | |
"02", | |
"03", | |
"04", | |
"08", | |
"09", | |
"072", | |
"073", | |
"074", | |
"076", | |
"077", | |
"050", | |
"052", | |
"054" | |
}; | |
int prefixLength = 0; | |
var foundMatchingPrefix = false; | |
foreach (var validPrefix in validPrefixes) | |
if (s.StartsWith(validPrefix)) | |
{ | |
prefixLength = validPrefix.Length; | |
foundMatchingPrefix = true; | |
break; | |
} | |
if (!foundMatchingPrefix) | |
return false; | |
// remaining length should be between 6 and 7 | |
var remainingLength = s.Length - prefixLength; | |
if (!((6 == remainingLength) || (7 == remainingLength))) | |
return false; | |
// check the rest of the string to be digits | |
for (int i = prefixLength; i < s.Length; i++) | |
if (!s[i].IsDigit()) | |
return false; | |
return true; | |
} | |
} | |
} |
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
public class PhoneNumber | |
{ | |
public static bool IsValid(string s) | |
{ | |
if (null == s) | |
return false; | |
var validPrefixes = new[] { | |
"02", | |
"03", | |
"04", | |
"08", | |
"09", | |
"072", | |
"073", | |
"074", | |
"076", | |
"077", | |
"050", | |
"052", | |
"054" | |
}; | |
int prefixLength = 0; | |
var foundMatchingPrefix = false; | |
foreach (var validPrefix in validPrefixes) | |
if (s.StartsWith(validPrefix)) | |
{ | |
prefixLength = validPrefix.Length; | |
foundMatchingPrefix = true; | |
break; | |
} | |
if (!foundMatchingPrefix) | |
return false; | |
if (s.Length == prefixLength) | |
return false; | |
int nextDigitIndex; | |
if (s[prefixLength] == ' ') | |
nextDigitIndex = prefixLength + 1; | |
else | |
nextDigitIndex = prefixLength; | |
// remaining length should be between 6 and 7 | |
var remainingLength = s.Length - nextDigitIndex; | |
if (!((6 == remainingLength) || (7 == remainingLength))) | |
return false; | |
// check the rest of the string to be digits | |
for (int i = nextDigitIndex; i < s.Length; i++) | |
if (!s[i].IsDigit()) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment