Implementation of universal validation in asp.net

Source: Internet
Author: User
Tags bool datetime split sql injection trim

Whether in B/S or C/s, data manipulation maintenance and retrieval, many times, all need to judge the validity of user input information. In b/s structure, the security of SQL injection may also be considered.

Since the problem is very common, we should have a business abstraction of the problem and get a unified solution. This article describes our approach to implement the universal validity test implemented in asp.net using the C # language.

We divide the whole process into three parts:

1, the format of data validation, this part should be committed to the framework of the independence of the structure, that is, whether the C/s structure or b/s structure, should be universal; Data security, since security considerations, in principle, b/S and C/s structure is also common, such as SQL The safety of injection;

2, in friendly interaction and friendly interface, it is necessary to provide an easy to understand calling interface and illegal reminders. This is a framework-related, different approach to web development and WinForm development. Since the title is the validity test in asp.net, we only discuss Web-mode interfaces.

3, page call, since it is a page call, of course, web-based.

Part I: Framework Versatility

For a framework-independent part, you should consider putting it in a base class, and we do so, so we can put it in a Fsdevcommon class that accumulates namespaces as Fscsharpdevfrk.common. The method of realizing the validity of the format speech data is as follows:

    public bool Checkvalidvalue (string strvalue, String strtypeformat) {bool Breturn = true;
        if (Strtypeformat!= null && strtypeformat.length > 0) {if (Strtypeformat.startswith ("D"))
          {string[] strtypes = Strtypeformat.split (', '); if (Strtypes.length > 1) {DateTime dtvalue = new DateTime (1900, 1, 1), Dtmaxvalue = new Dateti
            Me (2050, 12, 31);
            if (Strtypes.length > 2 && IsDate (strtypes[2)) Dtvalue = DateTime.Parse (strtypes[2));
  
            if (Strtypes.length > 3 && IsDate (strtypes[3)) Dtmaxvalue = DateTime.Parse (strtypes[3)); if (strtypes[1).
            Trim () = = ">") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) > Dtvalue); else if (strtypes[1).
    Trim () = = ">=") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) >= dtvalue);        else if (strtypes[1).
            Trim () = = "<") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) < dtvalue); else if (strtypes[1).
            Trim () = = "<=") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) < dtvalue); else if (strtypes[1).
            Trim () = = "=") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) = = Dtvalue); else if (strtypes[1). Trim (). ToUpper () = = "BETWEEN") Breturn = Isdigital (strvalue) && (DateTime.Parse (strvalue) >= dtvalue)
          && (DateTime.Parse (strvalue) <= dtmaxvalue);
        else Breturn = IsDate (strvalue);
          else if (Strtypeformat.startswith ("T")) {string[] strtypes = Strtypeformat.split (', '); if (Strtypes.length > 1) {DateTime dtvalue = new DateTime (1900, 1, 1), Dtmaxvalue = new D
  Atetime (2050, 12, 31);          if (Strtypes.length > 2 && isdatetime (strtypes[2)) Dtvalue = DateTime.Parse (strtypes[2
            ]);
  
            if (Strtypes.length > 3 && isdatetime (strtypes[3)) Dtmaxvalue = DateTime.Parse (strtypes[3)); if (strtypes[1).
            Trim () = = ">") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) > Dtvalue); else if (strtypes[1).
            Trim () = = ">=") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) >= dtvalue); else if (strtypes[1).
            Trim () = = "<") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) < dtvalue); else if (strtypes[1).
            Trim () = = "<=") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) < dtvalue); else if (strtypes[1). Trim () = = "=") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) = = Dtvalue); else if (strtypes[1). Trim ().  ToUpper () = = "BETWEEN") Breturn = Isdatetime (strvalue) && (DateTime.Parse (strvalue) >= dtvalue)
          && (DateTime.Parse (strvalue) <= dtmaxvalue);
        else Breturn = Isdatetime (strvalue);
          else if (Strtypeformat.startswith ("N")) {string[] strtypes = Strtypeformat.split (', ');
            if (Strtypes.length > 1) {Long LValue = 0, lmaxvalue = 99999999999999; if (Strtypes.length > 2 && isdigital (strtypes[2)) LValue = long.
            Parse (strtypes[2]); if (Strtypes.length > 3 && isdigital (strtypes[3)) Lmaxvalue = long.
  
            Parse (Strtypes[3]); if (strtypes[1). Trim () = = ">") Breturn = Isdigital (strvalue) && (int.
            Parse (strvalue) > LValue); else if (strtypes[1). Trim () = = ">=") Breturn = Isdigital (stRValue) && (int.
            Parse (strvalue) >= lValue); else if (strtypes[1). Trim () = = "<") Breturn = Isdigital (strvalue) && (int.
            Parse (strvalue) < lValue); else if (strtypes[1). Trim () = = "<=") Breturn = Isdigital (strvalue) && (int.
            Parse (strvalue) < lValue); else if (strtypes[1). Trim () = = "=") Breturn = Isdigital (strvalue) && (int.
            Parse (strvalue) = = LValue); else if (strtypes[1). Trim (). ToUpper () = = "BETWEEN") Breturn = Isdigital (strvalue) && (int. Parse (strvalue) >= lValue) && (int.
          Parse (strvalue) <= lmaxvalue);
        else Breturn = Isdigital (strvalue);
          else if (Strtypeformat.startswith ("F")) {string[] strtypes = Strtypeformat.split (', ');
            if (Strtypes.length > 1) {Double dvalue = 0, dmaxvalue = 999999999; If (Strtypes.length > 2 && isnumeric (strtypes[2])) Dvalue = Double.
            Parse (strtypes[2]); if (Strtypes.length > 3 && isdigital (strtypes[3)) Dmaxvalue = long.
  
            Parse (Strtypes[3]); if (strtypes[1). Trim () = = ">") Breturn = IsNumeric (strvalue) && double.
            Parse (strvalue) > Dvalue); else if (strtypes[1). Trim () = = ">=") Breturn = IsNumeric (strvalue) && double.
            Parse (strvalue) >= dvalue); else if (strtypes[1). Trim () = = "<") Breturn = IsNumeric (strvalue) && double.
            Parse (strvalue) < dvalue); else if (strtypes[1). Trim () = = "<=") Breturn = IsNumeric (strvalue) && double.
            Parse (strvalue) < dvalue); else if (strtypes[1). Trim () = = "=") Breturn = IsNumeric (strvalue) && double.
            Parse (strvalue) = = Dvalue); else if (strtypes[1). Trim (). ToUpper () = = "BETWEEN") Breturn = IsNumeric (strvalue) && double. Parse (strvalue) >= dvalue) && (double.
          Parse (strvalue) <= dmaxvalue);
        else Breturn = IsNumeric (strvalue);
          else if (Strtypeformat.startswith ("C")) {string[] strtypes = Strtypeformat.split (', ');
            if (Strtypes.length > 1) {string strval = "", Strmaxvalue = ""; if (Strtypes.length > 2 && strtypes[2]!= null && strtypes[2].
            Length > 0) strval = strtypes[2]; if (Strtypes.length > 3 && strtypes[3]!= null && strtypes[3].
  
            Length > 0) strmaxvalue = strtypes[3]; if (strtypes[1).
            Trim () = = ">") Breturn = IsNumeric (strvalue) && (Strvalue.compareto (Strval) > 0); else if (strtypes[1). Trim () = = ">=") Breturn = IsNumeric (Strvalue) && (Strvalue.compareto (strval) >= 0); else if (strtypes[1).
            Trim () = = "<") Breturn = IsNumeric (strvalue) && (Strvalue.compareto (Strval) < 0); else if (strtypes[1).
            Trim () = = "<=") Breturn = IsNumeric (strvalue) && (Strvalue.compareto (strval) <= 0); else if (strtypes[1).
            Trim () = = "=") Breturn = IsNumeric (strvalue) && (Strvalue.compareto (strval) = = 0); else if (strtypes[1). Trim (). ToUpper () = = "BETWEEN") Breturn = IsNumeric (strvalue) && (Strvalue.compareto (strval) >= 0)
            ;& (Strvalue.compareto (Strmaxvalue) <= 0); else if (strtypes[1). Trim (). ToUpper () = = "in") {if (Strval.length > 0) {if (STRMAXVALUE.L
                Ength > 0) breturn = isexists (strvalue, Stringsplit (Strval, Strmaxvalue)); else bReturn = Strvalue.indexof (strval) >= 0;
            else Breturn = false;
    }}} return Breturn; }

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.