Talking about the Method of Data Validity Calibration in ASP.NET

zhaozj2021-02-16  60

Talking about the method of data validity check in ASP.NET Post: Chi0591 Date: 2003-09-01 Popularity: 202 As a programmer, be sure to be responsible for the robustness of the program written by himself, so data verification Regardless of business logic or system implementation is an indispensable part. 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 {///

/// type base layer business rules /// public class BizObject {public const String REGEXP_IS_VALID_EMAIL = @ "^ / w ((- / w ) | (/. / )) * / @ / w ((/. | -) / w ) * /. / w $"; // Email verification Public const string regexp_is_valid_url = @ "^ http: // ([/ w-] /.) [/ w-] (/ [/ w- ./?%&=]*)?"; // URL Calibration constant public const string regexp_is_valid_zip = @ "/ d {6}"; // Zip code audited scheme public const string regexp_is_valid_ssn = @ "/ d {18} | / d {15}"; // ID card verification Public const string regexp_is_valid_int = @ "^ / d {1,} $"; // Integer Verifying the constant PUBLIC const string regexp_is_valid_demical = @ "^ -? (0 | / d ) (/. / D )?"; / / Numerical Verifying Factory "// Date Verifying the Umage Blue Const String Regexp_is_Valid_date = @" ^ (? :(? :(? :( ?: 1 [6-9] | [2-9] / d)? (? : 0 [48] | [2468] | [? :( ?: 16 | [2468] [048] | [3579])))) | - | /.) (?: 0? 2/1 (?: 29)) $) | (? :( ?: 1 [6-9] | [2-9] / d)? / D {2} ) (/ / | - | /.) (? :(? :( ?: 0? [13578] | 1 [02]) / 2 (?: 31)) | (? :( ?: 0? [1, 3-9] | 1 [0-2]) / 2 (29 | 30)) | (? :( ?: 0? [1-9]) | (?: 1 [0-2])) / 2 ( ?: 0? [1-9] | 1 / d | 2 [0-8])) $ "; public BizObject () {} #region check field is empty or field length long method public string getfieldtoolonger (string errorfield, int maxlen) {return errorfield ", please deduce" maxlen.toString () MAXLEN.TOSTRING () "A character! "} 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 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;}} return true;} #endregion #region Zip Code type field format check method public string GetZipFieldError (string ErrorField) {return ErrorField "incorrect format (157032)!";} public bool IsValidZip (DataRow Row, String FieldName, int M axLen, 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) The format is incorrect (length 15 or 18)! "

} Public bool IsValidSSN (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_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 "incorrect format (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, GetURLFIEL DERROR (ERRORFIELD)); returnfield;} #endregion #region check date Type field format method public string getdatefieldElderror (String Errorfield) {Return Errorfield "The date format is incorrect! ";} 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)); Return False;}} Return True;

} #endregion #Region check value type field format method // This is also a way to judge the value of the 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.SetColumnError (fieldName, GetFieldNullError (ErrorField)); return false;} else if ((isValid) && (i> 0)!) {Row.SetColumnError (fieldName, GetFieldNumberError (ErrorField)); return false;} return true } #endregion}} // Use the verification method ///

/// ////// ////////////////////////////////////////////// / Data row /// Parallel pass -false public bool validate (DATAROW ROW) {bool isvalid; row.clearerrors () Isvalid = isvalidfield (r OW, "Name", 20, "Name", False; IsValid & = IsValidzip (Row, "Zip", 6, "Zip Code"; IsValid & = IsValidNumber (Row, "Age", "Age", False); isvalid & = isvalidemail (Row, "Email", 50, "Email", true); Return IsValid;} // Display error message //// /// display submission data back in WebUI Error message /// private void displayerrors () {string fielderrors = ""; string tmpfielderrors = "; DATAROW ROW = DS.TABLES [0]. ROWS [0]; Foreach (Datacolumn Column In DS. Tables [0] .columns) {tmpfielderrors = row.getColumNerror (Column.columnName.toString ());

转载请注明原文地址:https://www.9cbs.com/read-18479.html

New Post(0)