As a programmer, we must be responsible for the robustness of the program written by yourself, so the verification of the data is an indispensable part of business logic or system implementation. 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 {///
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 format type field 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 #regi ON Code Code Type Form Format Method PUBLIC STRING GetZipfielderR (String Errorfield) {Return Errorfield "is not correct (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 Type Field Format Method PUBLIC STRING ERRORFIELD "The format is incorrect (length 15 or 18)!";} Public bool isvalidssn 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_ssn)). Ismatch (Row [FieldName] .tostring ()); if ((! Isvalid) && (i> 0)) {row.setColumNError (FieldName , GetSSNFieldError (ErrorField)); return false;}} return true;} #endregion #region check the URL format type field 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, geturlf); returnfield;}} Return true;} #endregion #Region check date Type field Format PUBLIC STRING GetDatefielderror (String Errorfield) {Return Errorfield "The date format is incorrect! "} Public bool isvaliddate (DATAROW ROW, STRING FIELDNAME, INT MAXLEN, STRING ERRORFIELD, BOOL All" {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); Return False;} #ENDREGION #Region Calibration Value Type Field Format Method // This is also a judgment value approval value private bool isnumeric (String value) {Try {Int i = int.parse (value); return true;} catCh {return false;}} public string GetFieldNumberError (string ErrorField) {return ErrorField "must be a number! (e.g.: 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. Setc olumnError (fieldName, GetFieldNullError (ErrorField)); return false;} else if ((isValid) && (i> 0)!) {Row.SetColumnError (fieldName, GetFieldNumberError (ErrorField)); return false;} return true;} # EndRegion}} // Use the verification method ///