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

xiaoxiao2021-03-06  85

Author: Unknown

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

{

///

/// Commercial rule layer base class

///

Public Class BizObject

{

Public const string regexp_is_valid_email = @ "^ / W (((- / w )) * / @ / w ((/. | -) / w ) * /. / w $"; // Electronics Mail verification

Public const string regexp_is_valid_url = @ "^ http: // ([/ w-] /.) [/ w-] (/ [/ w- ./?%&=]*)?"; // URL Calibration constant

Public const string regexp_is_valid_zip = @ "/ d {6}"; // Postal code verification

Public const string regExp_is_valid_ssn = @ "/ d {18} | / d {15}"; // ID card verification

Public const string regexp_is_valid_int = @ "^ / d {1,} $"; // Integer verification

Public const string regexp_is_valid_demical = @ "^ -? (0 | / D ) (/./ d )?"; // numeric verification

// Date verification

Public const string regexp_is_valid_date = @ "^ (? :(? :(? :( ?: 1 [6-9] | [2-9] / d)? (?: 0 [48] | [2468] [048] | [13579] | (? :( ?: 16 | [2468] [26]) 00))) (// | - | /.) (?: 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): PUBLIC STRINGELD, INT MAXLEN

{

Return Errorfield "Information is long, please deduce" maxlen.toString () "characters!";

}

Public string getfieldnullerror (string errorfield) {

Return Errorfield "is required, not allowed to be empty!"

}

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, GetfieldNuller (Errorfield));

Return False;

}

Else IF (i> maxlen)

{

Row.SetColumNerror (FieldName, GetfieldToolonGerror (Errorfield, Maxlen);

Return False;

}

Return True;

}

#ndregion

#Region Check Email Type Format Method

Public String getEmailFieldElderror (String Errorfield)

{

Return Errorfield "The format is incorrect (a@b.c)!";

}

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;

}

#ndregion

#Region check mail code type field format method

Public String getzipfielderror (String Errorfield)

{

Return Errorfield "The format is incorrect (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;

}

}

Return True;

}

#ndregion

#REGION check Identification card Type field format method

Public string getssnfielderror (String Errorfield)

{

Return 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;

}

#ndregion

#Region check URL Type field format method

Public String geturlfielderror (String Errorfield)

{

Return Errorfield "The format is incorrect (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;

}

}

Return True;

}

#ndregion

#Region Check Date Type Format Method

Public string getdatefielderror (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 (); 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;

}

#ndregion

#Region check value type field format method

// This is also a way to judge the 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, GetfieldNuller (Errorfield));

Return False;

}

Else IF ((! isvalid) && (i> 0))

{

Row.SetColumNerror (FieldName, GetfieldNumberrror (Errorfield));

Return False;

}

Return True;

}

#ndregion

}

}

/ / Inheriting the method of using the verification in the businessrule of the base class

///

/// Use the above method to verify the data

///

/// Data line

/// Pass - True does not pass - False

Public Bool Validate (DATAROW ROW)

{

Bool isvalid;

Row.clearerrors ();

Isvalid = isvalidfield (Row, "Name", 20, "Name", FALSE);

Isvalid & = isvalidzip (Row, Zip ", 6," Zip Code ", TRUE

Isvalid & = IsvalidNumber (Row, "Age", "age", false;

Isvalid & = IsValidemail (Row, "Email", 50, "Email", True; Return Isvalid;

}

/ / Display error prompt information in WebUI

///

// / Display 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 = Fielderror;

    }

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

    New Post(0)