-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
34 lines (31 loc) · 1.07 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace Regex_validate_PIN_code
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ValidatePin("1234\n"));
Console.WriteLine(ValidatePin("125"));
Console.WriteLine(ValidatePin("2234"));
Console.WriteLine(ValidatePin("王老五喔"));
Console.WriteLine(string.Empty);
Console.WriteLine(ValidatePin2("1234\n"));
Console.WriteLine(ValidatePin2("125"));
Console.WriteLine(ValidatePin2("2234"));
Console.WriteLine(ValidatePin2("王老五喔"));
}
public static bool ValidatePin(string pin)
{
//↓↓↓ First Practice
return Regex.IsMatch(pin, "\\D") ? false : Regex.IsMatch(pin, "^\\d{4}$|^\\d{6}$") || false;
}
//↓↓↓ Use Linq
public static bool ValidatePin2(string pin)
{
return (pin.Length == 4 || pin.Length == 6) && pin.All(c => char.IsDigit(c));
}
}
}