ASP. The service side verification of input verification in net

Source: Internet
Author: User
Tags bool exception handling numeric static class tostring trim visual studio

in ASP. NET platform. Users can use controls and regular expressions to implement input validation on the server side, which is relatively simple to validate controls and satisfies most requirements but to be honest, I'm not flattering them.

Microsoft Visual Studio validation controls are both client and server-side validation when we trigger validation, we automatically generate a lot of client-side scripting if we click on the button to trigger the validation and write some other script validation that might fail in the project. In order to prevent such accidents, someone mentioned With client-side validation but knocking over the code are aware of the client is not safe, so only the first server verification has time to add a client verification

Server-side validation

Question one: Verifying what

Date control Drop-down list multiple select radio and the most commonly used text boxes, and so on.

Whether it is empty is not the length of the numeric string, etc...

Question two: When do I start validation?

When to verify when you need to use the customer input but note that validation is not written everywhere we can define a method to specifically validate the input of a page

Problem Four: Exception handling

There are two kinds of exceptions, one of which is a logical anomaly. What we have to do is to deal with the logic exception. I wrote some verification in fact, the Internet is a lot of fart talk less to see Code!

Project Common Input Validation

Using System;
Using System.Collections.Generic;
Using System.Text.RegularExpressions; namespace Nadim.Verify.Common {///<summary>///parameter Validation///</summary> public static class Para
        meterverify {#region Verify string///<summary>///Verify that the string has illegal characters///</summary> <param name= "strinput" > Input string </param>///<returns> Pass Inspection </returns> Pub LIC static bool Verifystring (string strinput) {//Parameter check if (strinput = = NULL | | strinput.tr
            Im () = = "") return true; Check for illegal characters if (Regex.IsMatch (strinput) ([<> ' \ "]) | ( Delete from) |
            (drop table)) return false;
        else return true; #endregion #region Verify Date string///<summary>///Verify that the date string is legitimate///</summary&
        Gt <param name= "strinput" > Input string </param>///<Returns> whether the check </returns> public static bool Verifydate (string strinput) {if (Strinpu t = = NULL | |
            Strinput.trim () = = "") return false;
            try {convert.todatetime (strinput);
            catch (FormatException) {return false;
        return true;
        #endregion #region Check time string///<summary>///validation time string, validating format (YYYY-MM-DD hh:mm:ss) </summary>///<param name= "Strinput" > to verify the string </param>///<returns> pass the test </returns> public static bool Verifydatetime (string strinput) {//Parameter check if (s Trinput = = NULL | |
            Strinput.trim () = = "") return false; if (Regex.IsMatch (strinput, @) ^ (\d{4}) (-|/)? ( (0[1-9]) | (1[0-2]) (-|/)? (((0 [1-9]) | ([1-2][0-9]) | (3[0-1]) ([0-1][0-9]) | (2[0-3]):([0-5][0-9]):([0-5][0-9])) RETUrn true;
        else return false;
        #endregion #region Verify that the encoded string///<summary>///Verify that it is the correct encoded string, in the format: letters, Numbers, "-", "_" </summary>///<param name= "strinput" > Input character </param>///<param name= "MinSize" Minimum length </param>///<param name= "maxsize" > Max length </param>///<returns> Pass Inspection </retur
            ns> public static bool Verifyno (string strinput, int minsize, int maxsize) {//parameter checking
            if (strinput = null) return false; String expression = @ "^ ((\w) | ( _)| (-)| (\s)) {"+ MinSize +", "+ MaxSize + @"})
            $";
            if (Regex.IsMatch (strinput, expression)) return true;
        else return false;
        #endregion #region Verify numeric string///<summary>///Verify numeric string///</summary> <param name= "strinput" > Input character </param>///<param name= "Length" > Number length (not decimal points) </param>///<param name= "precision" > bit after decimal point Number </param>///<param name= "Allowminus" > whether it is allowed to be negative </param>///<returns> whether to pass the inspection </ret 
            urns> public static bool Verifydecimal (string strinput, int length, int precision, bool Allowminus) {
            String expression;
            Parameter Check if (strinput = null) return false; To determine if a negative number if (allowminus) expression = @ "^ (-{0,1}[0-9]{0," + (length-precision). ToString () + @ "}\. {0,1}|\. [0-9] {1, "+ precision. ToString () + @ "}|-{0,1}[0-9]{1," + (length-precision). ToString () + @ "}\. [0-9] {0, "+ precision.
            ToString () + @ "}) $"; else expression = @ "^" ([0-9]{0, "+ (length-precision)." ToString () + @ "}\. {0,1}|\. [0-9] {1, "+ precision. ToString () + @ "}|-{0,1}[0-9]{1," + (length-precision). ToString () + @ "}\. [0-9] {0, "+ precision. ToString () + 

@"})$";
            if (Regex.IsMatch (strinput, expression)) return true;
        else return false;
        #endregion #region Check variable-length string///<summary>///Check variable-length string///</summary>
        <param name= "strinput" > Input string </param>///<param name= "minsize" > Minimum length </param> <param name= "maxsize" > Max length </param>///<returns> Pass inspection </returns> public  static bool Verifyvarchar (string strinput, int minsize, int maxsize) {//Parameter check if (strinput
            = = null) return false;
                if (verifystring (strinput) && strinput.length >= minsize && strinput.length <= maxsize)
            return true;
        else return false;
        #endregion #region Verify the integer string///<summary>///Verify the integer string///</summary><param name= "strinput" > Input string </param>///<param name= "Allowminus" > whether to allow negative </param>
        <returns> whether through validation </returns> public static bool Verifyint (string strinput, bool Allowminus)
            {int i;
            Parameter Check if (strinput = null) return false;
                try {i = Convert.ToInt32 (Strinput.trim ());
                if (Allowminus = = False && I < 0) return false;
            else return true;
            catch (FormatException) {return false; }} #endregion}}

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.