asp.net| data
As a programmer, you must be responsible for the robustness of the program you write, so data validation is an essential part of both the business logic and the system implementation.
I have summed up a kind of asp.net (C #) Data verification method which is considered to be quite good, as we discussed.
This paper mainly uses the IsMatch method of regex to verify the validity of the data in the Businessrule layer, and officers transferred Guevara the method as part of the Businessrule layer base class.
In the WEBUI layer the realistic hint information.
Using System;
Using System.Data;
Using System.Text.RegularExpressions;
Namespace Education.businessrules
{
<summary>
base class of the business rule layer
</summary>
public class Bizobject
{
Public Const String Regexp_is_valid_email = @ "^\w+ (-\w+) | ( \.\w+)) *\@\w+ (\.| -) \w+) *\.\w+$ "; e-mail Checksum constants
Public Const String Regexp_is_valid_url = @ "^http://" ([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)? "; URL Checksum Constants
Public Const String Regexp_is_valid_zip = @ "\d{6}"; ZIP Code Check constant
Public Const String REGEXP_IS_VALID_SSN = @ "\d{18}|\d{15}"; ID Check constant
Public Const String Regexp_is_valid_int = @ "^\d{1,}$"; Integer Checksum constants
Public Const String regexp_is_valid_demical = @ "^-? (0|\d+) (\.\d+)? $ "; Numeric checksum Constants "
Date Check Constants
Public Const String Regexp_is_valid_date = @ "^ (?:(?:(?:(? : 1[6-9]| [2-9]\d]? (?: 0 [48]| [2468] [048]| [13579] [26]) | (?:(? : 16| [2468] [048]| [3579] [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 Verify that the field is empty or that the field length is too long
public string Getfieldtoolongerror (String Errorfield,int maxlen)
{
return Errorfield + "information is very long, please cut to" + MaxLen. ToString () + "characters! " ;
}
public string Getfieldnullerror (string Errorfield)
{
Return Errorfield + "is required and is 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, Getfieldnullerror (Errorfield));
return false;
}
else if (i > MaxLen)
{
Row.setcolumnerror (FieldName, Getfieldtoolongerror (Errorfield,maxlen));
return false;
}
return true;
}
#endregion
#region Verify E-mail Type field Format method
public string Getemailfielderror (string Errorfield)
{
return Errorfield + "format is not correct (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;
}
#endregion
#region Check Postcode Type field Format method
public string Getzipfielderror (string Errorfield)
{
return Errorfield + "is not in the correct 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;
}
}
return true;
}
#endregion
#region Verify ID Type field Format method
public string Getssnfielderror (string Errorfield)
{
return Errorfield + "is not in the correct format ( Length is 15 or 18 bits)! " ;
}
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 Validate URL Type field Format method
public string Geturlfielderror (string Errorfield)
{
return Errorfield + "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;
}
#endregion
#region Validation 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;
}
}
return true;
}
#endregion
#region Validate numeric Type field formatting methods
It's also a way of judging numbers.
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 (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;
}
return true;
}
#endregion
}
}
//using checksums in Businessrule that inherit base classes
///<summary>
///Use the above method to validate data
///</ Summary>
///<param name= "Row" > Data row </param>
///<returns> through--true not through--false </returns>
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, ages, false);
isvalid &= isvalidemail (Row, "email", 50, "e-mail", true);
return IsValid;
}
displaying error messages in WebUI
<summary>
Displays the error message returned by the submitted data
</summary>
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 + + "<li>" + tmpfielderrors + "<br>";
}
}
Display error messages
This.lblError.Text = fielderrors;
}