1. 程式人生 > >C#後臺程式碼使用正則表示式判斷是否符合要求

C#後臺程式碼使用正則表示式判斷是否符合要求

C#後臺使用正則表示式:

            string pattern = @"^[0-9a-zA-Z_]{1,10}$";//字母數字下劃線,1到10位
            bool result = false;
            if (!string.IsNullOrEmpty(this.txtNewPwd.Text.Trim()))
            {
                result = System.Text.RegularExpressions.Regex.IsMatch(this.txtNewPwd.Text, pattern);
                if (!result)
                {
                    throw new Exception("密碼為字母數字下劃線組合且最多10位");
                }
            }