Пример #1
0
        /// <summary>
        /// 數據類型驗證
        /// </summary>
        /// <param name="clsDataType">數據類型信息</param>
        /// <returns></returns>
        public static bool CheckDataType(CheckDataTypeValue clsDataType)
        {
            int l_iLength=clsDataType.strText.Length;
            bool l_blnReturn=false;
            switch(clsDataType.enuType)
            {
                case plc_ConstValue.g_enuCheckType.enuChinaChar:		//中文字符
                    if(l_iLength==0)
                    {
                        l_blnReturn= false;
                    }
                    else
                    {
                        int i=System.Text.Encoding.Default.GetBytes(clsDataType.strText).Length;
                        if(l_iLength!=i)
                        {
                            l_blnReturn= true;
                        }
                        else
                        {
                            l_blnReturn= false;
                        }
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuNumberChar:		//數字
                    try
                    {
                        l_blnReturn=true;
                        if(clsDataType.strText.ToString().Trim()!="")
                        {
                            Decimal Number=Convert.ToDecimal(clsDataType.strText);
                        }
                    }
                    catch(Exception Ex)
                    {
                        l_blnReturn=false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuDateChar:		//日期
                    try
                    {
                        l_blnReturn=true;
                        if(clsDataType.strText.ToString().Trim()!="")
                        {
                            DateTime Number=Convert.ToDateTime(clsDataType.strText);
                        }
                    }
                    catch(Exception Ex)
                    {
                        l_blnReturn=false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuEnglishChar:	//英文字符(不含符號)
                    CharEnumerator cha=clsDataType.strText.GetEnumerator();
                    l_blnReturn=true;
                    while(cha.MoveNext())
                    {
                        if('a'<=cha.Current && cha.Current<='z' || 'A'<=cha.Current && cha.Current<='Z')
                        {

                        }
                        else
                        {
                            l_blnReturn=false;
                            break;
                        }
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuNumeralString:	//數字字符(0-9)
                    CharEnumerator chr=clsDataType.strText.GetEnumerator();
                    l_blnReturn=true;
                    while(chr.MoveNext())
                    {
                        if('0'<=chr.Current && chr.Current<='9')
                        {

                        }
                        else
                        {
                            l_blnReturn=false;
                            break;
                        }
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuPlusNumberChar:		//正的數字
                    try
                    {
                        l_blnReturn=true;
                        if(clsDataType.strText.ToString().Trim()!="")
                        {
                            Decimal Number=Convert.ToDecimal(clsDataType.strText);
                            if(Number<0)
                                l_blnReturn=false;
                        }
                    }
                    catch(Exception Ex)
                    {
                        l_blnReturn=false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuPlusInt32:		//正的數字32
                    try
                    {
                        l_blnReturn = true;
                        if (clsDataType.strText.ToString().Trim() != "")
                        {
                            Int32 Number = Convert.ToInt32(clsDataType.strText);
                            Decimal sNumber = Convert.ToDecimal(clsDataType.strText);
                            if(Number!=sNumber)
                                l_blnReturn = false;
                            if (Number < 0)
                                l_blnReturn = false;
                        }
                    }
                    catch (Exception Ex)
                    {
                        l_blnReturn = false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuPlusInt64:		//正的數字64
                    try
                    {
                        l_blnReturn = true;
                        if (clsDataType.strText.ToString().Trim() != "")
                        {
                            Int64 Number = Convert.ToInt64(clsDataType.strText);
                            Decimal sNumber = Convert.ToDecimal(clsDataType.strText);
                            if (Number != sNumber)
                                l_blnReturn = false;
                            if (Number < 0)
                                l_blnReturn = false;
                        }
                    }
                    catch (Exception Ex)
                    {
                        l_blnReturn = false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuInt32:		//數字32
                    try
                    {
                        l_blnReturn = true;
                        if (clsDataType.strText.ToString().Trim() != "")
                        {
                            Int32 Number = Convert.ToInt32(clsDataType.strText);
                            Decimal sNumber = Convert.ToDecimal(clsDataType.strText);
                            if (Number != sNumber)
                                l_blnReturn = false;
                        }
                    }
                    catch (Exception Ex)
                    {
                        l_blnReturn = false;
                    }
                    break;
                case plc_ConstValue.g_enuCheckType.enuInt64:		//數字64
                    try
                    {
                        l_blnReturn = true;
                        if (clsDataType.strText.ToString().Trim() != "")
                        {
                            Int64 Number = Convert.ToInt64(clsDataType.strText);
                            Decimal sNumber = Convert.ToDecimal(clsDataType.strText);
                            if (Number != sNumber)
                                l_blnReturn = false;
                        }
                    }
                    catch (Exception Ex)
                    {
                        l_blnReturn = false;
                    }
                    break;
                default:

                    break;
            }
            return l_blnReturn;
        }
Пример #2
0
        /// <summary>
        /// 數據類型驗證
        /// </summary>
        /// <param name="strText">驗證內容</param>
        /// <param name="enuCheckTye">驗證類型</param>
        /// <returns></returns>
        public static bool CheckDataType(string strText,plc_ConstValue.g_enuCheckType enuCheckTye)
        {
            bool l_bolTrue=false;
            string l_strMessage="";
            CheckDataTypeValue DataValue=new CheckDataTypeValue();
            DataValue.strText=strText;
            DataValue.enuType=enuCheckTye;

            try
            {
                l_bolTrue=plc_MainClass.CheckDataType(DataValue);
            }
            catch(Exception Ex)
            {
                l_bolTrue=false;
                throw(new Exception(Ex.Message));
            }

            switch(enuCheckTye)
            {
                case plc_ConstValue.g_enuCheckType.enuChinaChar:
                    if(l_bolTrue)
                    {
                        l_strMessage="祥褫眕怀⻌笢恅趼,ワ潰脤!";
                        l_bolTrue=false;
                    }
                    else
                    {
                        l_bolTrue=true;
                    }
                    break;

                case plc_ConstValue.g_enuCheckType.enuDateChar:
                    if(!l_bolTrue)
                    l_strMessage="ワ怀⻌゜ヽ!";
                    break;

                case plc_ConstValue.g_enuCheckType.enuNumberChar:
                    if(!l_bolTrue)
                    l_strMessage="ワ怀⻌杅趼!";
                    break;

                case plc_ConstValue.g_enuCheckType.enuEnglishChar:
                    if(!l_bolTrue)
                        l_strMessage="ワ怀⻌曾荎恅趼譫!";
                    break;

                case plc_ConstValue.g_enuCheckType.enuNumeralString:
                    if(!l_bolTrue)
                        l_strMessage="ワ怀⻌曾杅趼0-9!";

                    break;

                case plc_ConstValue.g_enuCheckType.enuPlusNumberChar:
                    if(!l_bolTrue)
                        l_strMessage="ワ怀⻌淏硉杅趼!";

                    break;
                case plc_ConstValue.g_enuCheckType.enuPlusInt32:
                    if (!l_bolTrue)
                        l_strMessage = "ワ怀⻌淏淕杅!";
                    break;
                case plc_ConstValue.g_enuCheckType.enuInt32:
                    if (!l_bolTrue)
                        l_strMessage = "ワ怀⻌淕杅!";
                    break;
                default:
                    break;

            }
            if(l_strMessage.Trim().Length>0)
            {
                MessageBox.Show(l_strMessage, DefineConstantValue.SystemMessageText.strMessageTitle + DefineConstantValue.SystemMessageText.strWarning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            return l_bolTrue;
        }