Geeks With Blogs

News


Google RankGoogle PR™ - Post your Page Rank with MyGooglePageRank.com



The content on this site represents my own personal opinions and thoughts at the time of posting, and does not reflect those of my employer's in any way.

Disclaimer:- All postings in this blog is provided "AS IS" with no warranties, and confers no rights.
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(@"^(([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") + "'/>");

                                        }

                                    }

                                }

 

                            }

 

                            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();

 

                            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");

 

                            if (drValidation["value"].ToString().Trim().Length > 0)

                            {

                                if (strFlags.Length.Equals(2))

                                {

                                    if (regExpression.IsMatch(strFlags[0].ToString().Trim()) &&

                                                regExpression.IsMatch(strFlags[0].ToString().Trim()))

                                    {

                                        dtStartDate = Convert.ToDateTime(strFlags[0].ToString().Trim());

                                        dtEndDate = Convert.ToDateTime(strFlags[1].ToString().Trim());

 

                                        dtStartDate = dtStartDate.Date;

                                        dtEndDate = dtEndDate.Date;

                                        if (DateTime.Compare(dtStartDate, dtEndDate) < 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;

        }

 

Posted on Thursday, March 6, 2008 3:30 AM Validation | Back to top


Comments on this post: Server Side Validation

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


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net