Geeks With Blogs
Gaurav Taneja Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.
public bool CheckError()
        { //check error for all records
            strError.Append("<Result>0</Result><ErrorMessages>");
            bool blnReturnVal = true;
            for (int i = 0; i < dtbValidate.Rows.Count; i++)
            {
                DataRow drValidation = dtbValidate.Rows[i];
                string command = drValidation["command"].ToString().ToLower();

                # region validation check
                switch (command)
                {
                    case "required":
                        {
                            if (drValidation["value"].ToString().Trim().Length <= 0)
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "greater":
                        {
                            if (Convert.ToInt64(drValidation["value"].ToString()) >
                                Convert.ToInt64(drValidation["conditionValue"].ToString().Trim()))
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }

                    case "maxlen":
                        {
                            int intMaxLength = 0;
                            try { intMaxLength = Convert.ToInt32(drValidation["conditionValue"].ToString().Trim()); }
                            catch { }
                            if (drValidation["value"].ToString().Trim().Length > intMaxLength)
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "minlen":
                        {
                            int intMinLength = 0;
                            try { intMinLength = Convert.ToInt32(drValidation["conditionValue"].ToString().Trim()); }
                            catch { }
                            if (drValidation["value"].ToString().Trim().Length < intMinLength)
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "arrlen":
                        {
                            int intMinLength = 1;
                            try { intMinLength = Convert.ToInt32(drValidation["conditionValue"].ToString().Trim()); }
                            catch { }
                            if (Convert.ToInt32(drValidation["value"].ToString()) < intMinLength)
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "notequal":
                        {
                            if (drValidation["value"].ToString() != drValidation["conditionValue"].ToString())
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "flag":
                        {
                            bool blnValidFlag = false;
                            string[] strFlags = drValidation["conditionValue"].ToString().Split('|');

                            if (!string.IsNullOrEmpty(drValidation["value"].ToString().Trim()))
                            {
                                for (int intFlag = 0; intFlag < strFlags.Length; intFlag++)
                                {
                                    if (drValidation["value"].ToString().ToUpper().Equals(strFlags[intFlag].ToString().ToUpper()))
                                    {
                                        blnValidFlag = true;
                                        break;
                                    }
                                }

                                if (!blnValidFlag)
                                {
                                    blnReturnVal = false;
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                }
                            }

                            break;
                        }
                    case "equal":
                        {
                            if (drValidation["value"].ToString() == drValidation["conditionValue"].ToString())
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "lessthan":
                        {
                            if (Convert.ToInt64(drValidation["value"].ToString()) <
                                Convert.ToInt64(drValidation["conditionValue"].ToString()))
                            {
                                strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                blnReturnVal = false;
                            }
                            break;
                        }
                    case "alphanumeric":
                        {
                            Regex regExpression = new Regex("[^A-Za-z0-9]");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (regExpression.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "numeric":
                        {
                            Regex regExpression = new Regex("[^0-9.]");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (regExpression.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "alphabetic":
                        {
                            Regex regExpression = new Regex("[^A-Za-z ]");

                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (regExpression.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "alnumhyphen":
                        {
                            Regex regExpression = new Regex("[^A-Za-z0-9\\-_]");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (regExpression.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "email":
                        {
                            Regex regExpression = new Regex("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (!regExpression.IsMatch(drValidation["value"].ToString().Trim()))
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "date":
                        {
                            Regex regExpression = new Regex(@"(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (!regExpression.IsMatch(drValidation["value"].ToString().Trim()))
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "time":
                        {
                            //Regex regExpression = new Regex(@"^(\d{2}):(\d{2})\s{1}(([aA])|([pP]))[mM]$");
                            //new Regex(@"^(\d{2}):(\d{2})\s{1}([AP]M)$");
                            // ^(([1-9]{1})|([0-1][1-2])|(0[1-9])|([1][0-2])):([0-5][0-9])(([aA])|([pP]))[mM]$
                            Regex regExpression = new Regex(@"^(([1-9]{1})|([0-1][1-2])|(0[1-9])|([1][0-2])):([0-5][0-9])\s{1}(([aA])|([pP]))[mM]$");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (!regExpression.IsMatch(drValidation["value"].ToString().Trim()))
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "validtime":
                        {
                            //bool blnValidFlag = false;
                            DateTime dtStartTime = new DateTime();
                            DateTime dtEndTime = new DateTime();
                            DateTime dtNewDate = new DateTime();
                            string strStartTime = string.Empty;
                            string strEndTime = string.Empty;
                            string strStartPM = string.Empty;
                            string strEndPM = string.Empty;
                            string[] strFlags = drValidation["conditionValue"].ToString().Split('|');

                            Regex regExpression = new Regex(@"^(([1-9]{1})|([0-1][1-2])|(0[1-9])|([1][0-2])):([0-5][0-9])\s{1}(([aA])|([pP]))[mM]$");



                            if (!string.IsNullOrEmpty(drValidation["value"].ToString().Trim()))
                            {
                                if (strFlags.Length.Equals(2))
                                {
                                    strStartTime = strFlags[0].ToString().ToUpper();
                                    strEndTime = strFlags[1].ToString().ToUpper();



                                    if (regExpression.IsMatch(strStartTime.ToString().Trim()) &&
                                        regExpression.IsMatch(strEndTime.ToString().Trim()))
                                    {
                                        strStartPM = strStartTime.Substring(strStartTime.IndexOf(" ")).Trim();
                                        strEndPM = strEndTime.Substring(strEndTime.IndexOf(" ")).Trim();

                                        dtStartTime = Convert.ToDateTime(strStartTime);
                                        dtEndTime = Convert.ToDateTime(strEndTime);

                                        if (strStartPM.ToUpper().Equals("PM"))
                                        {
                                            if (strEndPM.ToUpper().Equals("AM"))
                                            {
                                                dtEndTime = dtEndTime.AddDays(1);
                                            }
                                        }

                                        TimeSpan tsTimeDiff = dtEndTime.Subtract(dtStartTime);

                                        if (tsTimeDiff.Minutes < 0 || tsTimeDiff.Hours < 0)
                                        {
                                            blnReturnVal = false;
                                            strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                        }
                                        else if (dtStartTime.CompareTo(dtEndTime) == 0)
                                        {
                                            blnReturnVal = false;
                                            strError.Append("<Error code='ERR6029' description='" + GetCustomError("ERR6029") + "'/>");
                                        }
                                    }
                                }
                                else
                                {
                                    // blnReturnVal = false;
                                    //  strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                }

                            }

                            break;
                        }
                    case "phone":
                        {
                            Regex regExpression = new Regex("[^0-9\\-+()/. ]");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                                if (regExpression.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            break;
                        }
                    case "password":
                        {
                            int intValid = 0;
                            Regex regExpSmallChar = new Regex("[a-z ]");
                            Regex regExpUpperChar = new Regex("[A-Z ]");
                            Regex regExpNumeric = new Regex("[0-9]");
                            if (drValidation["value"].ToString().Trim().Length > 0)
                            {
                                if (regExpSmallChar.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                {
                                    intValid++;
                                    if (regExpUpperChar.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                    {
                                        intValid++;
                                        if (regExpNumeric.Matches(drValidation["value"].ToString().Trim()).Count > 0)
                                        {
                                            intValid++;
                                        }
                                    }
                                }

                                if (intValid < 3)
                                {
                                    strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                    blnReturnVal = false;
                                }
                            }
                            break;
                        }
                    case "datediff":
                        {
                            DateTime dtStartDate = new DateTime();
                            DateTime dtEndDate = new DateTime();
                            DateTime dtExpStartDate = new DateTime();
                            DateTime dtExpEndDate = new DateTime();
                            int intYear;
                            int intMonth;
                            int intDay;
                            int intYear1;
                            int intMonth1;
                            int intDay1;


                            string[] strFlags = drValidation["conditionValue"].ToString().Split('|');

                            //Regex regExpression = new Regex(@"(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d");
                            Regex regExpression = new Regex(@"(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d");

                            if (drValidation["value"].ToString().Trim().Length > 0)
                            {
                                if (strFlags.Length.Equals(2))
                                {
                                    if (regExpression.IsMatch(strFlags[0].ToString().Trim()) &&
                                                regExpression.IsMatch(strFlags[1].ToString().Trim()))
                                    {
                                        intYear = int.Parse(strFlags[0].ToString().Substring(strFlags[0].ToString().LastIndexOf("/") + 1));
                                        intMonth = int.Parse(strFlags[0].ToString().Substring(strFlags[0].ToString().IndexOf("/") + 1, 2));
                                        intDay = int.Parse(strFlags[0].ToString().Substring(0, 2));
                                        dtExpStartDate = new DateTime(intYear, intMonth, intDay);

                                        intYear1 = int.Parse(strFlags[1].ToString().Substring(strFlags[1].ToString().LastIndexOf("/") + 1));
                                        intMonth1 = int.Parse(strFlags[1].ToString().Substring(strFlags[1].ToString().IndexOf("/") + 1, 2));
                                        intDay1 = int.Parse(strFlags[1].ToString().Substring(0, 2));
                                        dtExpEndDate = new DateTime(intYear1, intMonth1, intDay1);

                                        if (DateTime.Compare(dtExpEndDate, dtExpStartDate) < 0)
                                        {
                                            strError.Append("<Error code='" + drValidation["errCode"].ToString() + "' description='" + drValidation["errMessage"].ToString().Replace("~~", drValidation["fieldName"].ToString()) + "'/>");
                                            blnReturnVal = false;
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    default:
                        break;
                }
                # endregion

            }
            strError.Append("</ErrorMessages>");
            return blnReturnVal;
        }

        #endregion

        #endregion
    } Posted on Thursday, March 13, 2008 11:56 PM Validation | Back to top


Comments on this post: General Validation Hint in CS File

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net