I have summarized a data check method that self-think of better ASP.NET (C #), such as everyone discussion.
Mainly used Regex's ISMATCH method, the validity of the data contained data is performed in the BusinessRule layer, and the verification method is part of the BusnesRule layer base class.
Realistic tips in the WebUI layer.
using System; using System.Data; using System.Text.RegularExpressions; namespace Education.BusinessRules {///
Public BizObject () {}
#REGION check field is empty or field length long method
Public String getfieldtoolongerror (string errorfield, int maxlen) {return errorfield "information is long, please deduce" maxlen.toString () "characters!";
Public String getfieldnuller (String Errorfield) {Return Errorfield "is required, not allowed!";
Public Bool Isvalidfield (DataRow Row, String FieldName, Int Maxlen, String Errorfield, Bool Allownull) {INT I = (SHORT) (Row [FieldName] .tostring (). Trim (). Length); IF (i <1 && ! AllowNull)) {Row.SetColumnError (fieldName, GetFieldNullError (ErrorField)); return false;} else if (i> maxLen) {Row.SetColumnError (fieldName, GetFieldTooLongError (ErrorField, maxLen)); return false;} return true; } #endregion
#Region Check Email Type Format Method
public string GetEmailFieldError (string ErrorField) {return ErrorField "Malformed (a @ bc)!";} public bool IsValidEmail (DataRow Row, String fieldName, int maxLen, string ErrorField, bool AllowNull) {int i = (short) (Row [FieldName] .tostring (). Trim (). Length);
Bool isvalid = isvalidfield (Row, FieldName, Maxlen, Errorfield, Allownull);
IF (isvalid) {isvalid = (new regex (regexp_is_valid_email). Ismatch (Row [FieldName] .tostring ());
IF ((! isvalid) && (i> 0)) {Row.SetColumNError (FieldName, GetemailFielderror (Errorfield)); Return False;}} #ENDREGION
#Region check mail code type field format method
public string GetZipFieldError (string ErrorField) {return ErrorField "incorrect format (157032)!";} public bool IsValidZip (DataRow Row, String fieldName, int maxLen, string ErrorField, bool AllowNull) {int i = (short) (Row [FieldName] .tostring (). Trim (). Length);
Bool isvalid = isvalidfield (Row, FieldName, Maxlen, Errorfield, Allownull);
IF (isvalid) {isvalid = (new regex (regexp_is_valid_zip)). Ismatch (Row [FieldName] .tostring ());
IF ((! isvalid) && (i> 0)) {row.setColumNError (FieldName, getzipfielderror (Errorfield)); Return False;}} #ENDREGION
#REGION check Identification card Type field format method
public string GetSSNFieldError (string ErrorField) {return ErrorField "incorrect format (length of 15 or 18)!";} public bool IsValidSSN (DataRow Row, String fieldName, int maxLen, string ErrorField, bool AllowNull) {int i = () (Row [FieldName] .tostring (). Trim (). Length); Bool Isvalid = IsvalidField (Row, FieldName, Maxlen, Errorfield, Allownull);
IF (isvalid) {isvalid = (new regex (regexp_is_valid_ssn)). Ismatch (Row [FieldName] .tostring ());
IF ((isvalid) && (i> 0)) {row.setColumNError (FieldName, getssnfielderror (Errorfield); Return False;}} #endregion
#Region check URL Type field format method
public string GetUrlFieldError (string ErrorField) {return ErrorField "Malformed (http://www.abc.com)!";} public bool IsValidUrl (DataRow Row, String fieldName, int maxLen, string ErrorField, bool AllowNull) { INT I = (short) (row [FieldName] .tostring (). Trim (). Length);
Bool isvalid = isvalidfield (Row, FieldName, Maxlen, Errorfield, Allownull);
IF (isvalid) {isvalid = (new regex (regexp_is_valid_url). Ismatch (Row [FieldName] .tostring ());
IF ((! isvalid) && (i> 0)) {Row.SetColumNError (FieldName, geturlfielderror (ERRORFIELD)); Return False;}} #ENDREGION
#Region Check Date Type Format Method
public string GetDateFieldError (string ErrorField) {return ErrorField "Date format is not correct!";} public bool IsValidDate (DataRow Row, String fieldName, int maxLen, string ErrorField, bool AllowNull) {int i = (short) (Row [fieldName ] .ToString (). Trim (). Length);
Bool isvalid = isvalidfield (Row, FieldName, Maxlen, Errorfield, Allownull);
IF (isvalid) {isvalid = (new regex (regexp_is_valid_date))). Ismatch (Row [FieldName] .tostring ());
IF ((! isvalid) && (i> 0)) {Row.SetColumNerror (FieldName, getDatefielderror (Errorfield)); returnfield;}} Return true;} # endregion # region check numeric type field format method // This is also Method for judging Value Private Bool IsNumeric (String Value) {Try {Int i = int.parse (Value); Return True;} catCh {returnaf false;}}}
Public string getfieldnumberrror (string errorfield) {return errorfield must be a number (for example: 90)! ";
Public Bool IsValidNumber (DataRow Row, String FieldName, String Errorfield, Bool Allownull) {INT I = (SHORT) (Row [FieldName] .tostring (). Trim (). Length;
Bool isvalid = (new regex (regexp_is_valid_demical)). Ismatch (Row [FieldName] .tostring ());
if (i <1 && (AllowNull)!) {Row.SetColumnError (fieldName, GetFieldNullError (ErrorField)); return false;} else if (! (isValid) && (i> 0)) {Row.SetColumnError (fieldName, GetFieldNumberError (Errorfield); Return False;} #ENDREGION
}
/ / Inheriting the data using the verification method /// // Display error message /// DATAROW ROW = DS.TABLES [0]. Rows [0]; Foreach (Datacolumn Column In Ds.Tables [0] .COLUMNS) {TmpfielderRors = ROW.GETCOLUMNERROR (Column.columnname.toString ()); if (TmpFielderRRORS! = "") {FIELDERRORS = "
";}} // Display Error message this.lblerror.text = FIELDERRORS;