Talking about the method of data validity check in ASP.NET!

xiaoxiao2021-03-06  104

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 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 verification email type field 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 identity 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 = (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;}}} #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;}} # endr} # endregion # region check date type field 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); Return False;}} #ENDREGION

#Region check value type field format method // This is also a way to judge value private bool isnumeric (string value) {Try {Int i = int.parse (value); return true;} catCh {return 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}} #ENDREGON}}

/ / Inheriting the data using the verification method ///

/// ////// ///

// Display error message ///

/// display in WebUI Shows the error message returned by the submitted data /// 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 ()); if (! tmpfieldErrors = "") {fieldErrors = "

  • " tmpfieldErrors "
    ";}} // Display error message this.lblerror.text = FieldErrorS;

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

    New Post(0)