asp.net| data as a programmer, you must be responsible for the robustness of the program you write, so data verification is an essential part of both business logic and 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);
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 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);
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);
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
}
}
Methods for using checksums in Businessrule that inherit from the base class
<summary>
Validate data using the above method
</summary>
<param name= "Row" > Data rows </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", 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= "";
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.