using System;
namespace Sp.Common
{
public class ValidatorHelper
{
/// <summary>
/// Verify IPv4 address
/// [最初と最後の数字は0または255にすることはできません。0の補数を使用できます]
// / </ summary>
/// <param name = "input"> 検証する文字列</ param>
/// <returns> 一致</ returns>
public static bool IsIPv4(string input)
{
//文字列パターン= @ "^(25 [0-4] | 2 [0-4] \ d] | [01]?\ d {2} | [1-9])\。(25 [0-5] | 2 [0-4] \ d] | [01]?\ d?\ d)\。(25 [0-5] | 2 [0-4] \ d] | [01]?\ d?\ d) \。(25 [0-4] | 2 [0-4] \ d] | [01]?\ d {2} | [1-9])$ ";
// IsMatch(input、pattern);を返します
string [] IPs = input.Split(' 。' );
if(IPs.Length!= 4 )は
falseを返し ます。
int n = -1 ;
for(int i = 0 ; i <IPs.Length; i ++ )
{
if(i == 0 || i == 3)
{
if(int .TryParse(IPs [i]、out n)&& n> 0 && n < 255 )
続行;
それ以外の場合は
falseを返し ます。
}
else
{
if(int .TryParse(IPs [i]、out n)&& n> = 0 && n <= 255 )
続行;
それ以外の場合は
falseを返し ます。
}
}
戻る 真;
}
}
}