protected bool AddError(TextBox _control, TextBox _control_if_not_empty, string _message, bool _error, ValidationRestriction _restrict, HtmlGenericControl ul, Panel _panel) { _control.CssClass = _control.CssClass.Replace(" invalid", ""); if (_control_if_not_empty.Text.Trim() != "") { return(AddError(_control, _message, _error, _restrict, Validation.Required, ul, _panel)); } else { return(_error); } }
protected bool AddError(TextBox _control, string _message, bool _error, ValidationRestriction _restrict, Validation _type, HtmlGenericControl ul, Panel _panel) { _control.CssClass = _control.CssClass.Replace(" invalid", ""); bool error = false; string strValue = _control.Text.Trim(); switch (_restrict) { case ValidationRestriction.Basic: if (_type == Validation.Required) { error = (strValue == ""); } break; case ValidationRestriction.Numeric: if (_type == Validation.Required && strValue == "") { error = true; } else if (strValue != "" && ParseInt(strValue) < 0) { error = true; } break; case ValidationRestriction.IPAddress: if (_type == Validation.Required && strValue == "") { error = true; } else if (strValue != "") { if (strValue.Contains(".") == false) { error = true; } else { string[] strValues = strValue.Split(new char[] { '.' }); if (strValues.Length != 4) { error = true; } else { int add1 = ParseInt(strValues[0]); int add2 = ParseInt(strValues[1]); int add3 = ParseInt(strValues[2]); int add4 = ParseInt(strValues[3]); if ((add1 < 1 || add1 > 255) || (add2 < 0 || add2 > 255) || (add3 < 0 || add3 > 255) || (add4 < 0 || add4 > 255) ) { error = true; } } } } break; case ValidationRestriction.MACAddress: if (_type == Validation.Required && strValue == "") { error = true; } else if (strValue != "") { if (strValue.Contains(":") == false) { error = true; } else { string[] strValues = strValue.Split(new char[] { ':' }); if (strValues.Length != 6) { error = true; } else { foreach (string _value in strValues) { if (_value.Length != 2) { error = true; break; } } } } } break; } if (error == true) { _control.CssClass += " invalid"; _error = AddError(_control, _message, ul, _panel); } else { _control.Text = strValue; } return(_error); }