oracle常用字段正则表达式验证

<pre name="code" class="csharp">using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    /// <summary>
    /// 验证类
    /// </summary>
    public class Verification
    {
        /// <summary>
        /// 验证字符串是否满足 Varchar2(N)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="N"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns></returns>
        public static bool IsVarchar2N(string str, int N, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
                return Convert.ToBoolean(GetLength(str) > N ? bool.FalseString : bool.TrueString);

        }


        /// <summary>
        /// 验证是否满足数据类型 Number(N)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="N"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns>验证通过返回True</returns>
        public static bool IsNumberN(string str, int N, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                N = N - 1;
                //^[1-9]\d{0,3}$
                if (Regex.IsMatch(str, string.Format(@"^0|[1-9]\d{{0,{0}}}$", N))) //0 或非0开头的数字
                    return true;
                return false;
            }
        }


        /// <summary>
        /// 验证是否满足数据类型 Number(M,N)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <param name="M">整数位最多可以有M位</param>
        /// <param name="N">小数位最多可有N位整数</param>
        /// <returns>验证通过返回True</returns>
        public static bool IsNumberMN(string str, int M, int N, int Nullable)
        {
            //^[-\+]{0,1}\d{1,12}(\.\d{1,3})?$
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                string regStr = string.Format(@"^[-\+]?\d{{1,{0}}}(\.\d{{1,{1}}})?$", M, N);
                if (Regex.IsMatch(str, regStr))
                    return true;
                return false;
            }


            //M = M - 1;//第一位的0或非0要占1位 M值表示第一位之后的整数位个数
            ^(0|[1-9][0-9]{0,2})(.[0-9]{1,3})?$

            //if (string.IsNullOrWhiteSpace(str))
            //{
            //    return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            //}
            //else
            //{
            //    string regStr = @"^(0|[1-9][0-9]{0," + M + "})(.[0-9]{1," + N + "})?$";
            //    if (Regex.IsMatch(str, regStr))
            //    {
            //        return true;
            //    }
            //    return false;
            //}
        }



        /// <summary> 
        /// 获取字符串长度,一个汉字算两个字节 
        /// </summary> 
        /// <param name="str"></param> 
        /// <returns></returns> 
        private static int GetLength(string str)
        {
            if (str.Length == 0) return 0;
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0; byte[] s = ascii.GetBytes(str);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                    tempLen += 2;
                else
                    tempLen += 1;
            }
            return tempLen;
        }


        /// <summary>
        /// 验证是否满足邮箱规则
        /// 合法数据实例:1.1@1.1.1
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns></returns>
        public static bool IsMail(string str, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                if (Regex.IsMatch(str, @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"))
                    return true;
                return false;
            }
        }



        /// <summary>
        /// 验证是否符合电话号或是手机号规则
        /// 1、可以是1开头的11位数字(手机号)
        /// 2、可以是“区号-电话号-分机号”或者是“(区号)电话号-分机号”格式
        /// 3、区号是0开头的3~4位数字,可以没有区号
        /// 4、电话号是5~8位数字,不能以0开头
        /// 5、分机号是1~8位数字,可以没有分机号
        /// 合法数据示例:13812341234,021-12345678,(0451)1234567-1234
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns></returns>
        public static bool IsPhoneNumber(string str, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                if (Regex.IsMatch(str, @"^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$"))
                    return true;
                return false;
            }
        }


        /// <summary>
        /// 验证是否是符合传真规则(国内传真)
        /// 有效数据实例:021-12345678
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns></returns>
        public static bool IsFax(string str, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                if (Regex.IsMatch(str, @"^(\d{3,4}-)?\d{7,8}$"))
                    return true;
                return false;
            }
        }


        /// <summary>
        /// 验证是否符合邮编规则(6位数字即可)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Nullable">是否允许为空 1允许 0不允许</param>
        /// <returns></returns>
        public static bool IsPostCode(string str, int Nullable)
        {
            if (string.IsNullOrWhiteSpace(str))
                return Convert.ToBoolean(Nullable == 1 ? bool.TrueString : bool.FalseString);
            else
            {
                if (Regex.IsMatch(str, @"^\d{6}$"))
                    return true;
                return false;
            }
        }
    }
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值