정규식(유효성 체크)

1. 소스
using System.Text.RegularExpressions;
static void Main(string[] args)
{
        string str = "abcwDW2";
        Regex regex = new System.Text.RegularExpressions.Regex(@"^[0-9a-zA-Z]{1,100}$");

        Boolean ismatch = regex.IsMatch(str);
        if (!ismatch)
        {
            Console.WriteLine("숫자와 영문 소문자,대문자만 입력가능합니다.(글자제한100)");
        }
}

2. 문자열이 숫자 형식인지를 확인하는 함수
public bool IsNumber(string strValue)
{
     if (strValue == null || strValue.Length < 1)
        return false;


     Regex reg = new Regex(@"^(\d)+$");


     return reg.IsMatch(strValue);
}

3.  문자열이 알파벳으로만 구성되어 있는지를 확인하는 함수
 public bool IsAlpabet(string strValue)
 {
     if (strValue == null || strValue.Length < 1)
        return false;



     Regex reg = new Regex(@"^[a-zA-Z]+$");
     return reg.IsMatch(strValue);
 }

4. 시작문자열이 알파벳이고, 알파벳과 숫자로 이루어진 문자열인지 여부 확인하는 함수
  public bool IsAlpaNumber(string strValue)
  {
     if (strValue == null || strValue.Length < 1)
        return false;


     Regex reg = new Regex(@"^[a-zA-Z]+[0-9]*$");
     return reg.IsMatch(strValue);
  }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Previous
Next Post »