Form Control Verification

Source: Internet
Author: User

Custom a Form Control Verification class

/// <Summary> /// Form Control Verification /// </Summary> public class winformvalidating {/// <summary> /// path verification /// </ summary> // <returns> </returns> Public static bool validtepath (control Ctrl, errorprovider EP) {string pattern = @ "^ [A-Za-Z] :( (\ (?!) [^ /:*? <> \ "" | \] +) + \\?) | (\\)?) \ S * $ "; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (CTRL. Text); If (! ISR) EP. seterror (CTRL, "The entered path is invalid"); Return ISR ;} /// <summary> /// verify whether it is empty /// </Summary> /// <Param name = "Ctrl"> </param> /// <Param name = "EP"> </param> // <returns> </returns> Public static bool validatenull (control Ctrl, errorprovider EP) {string content = ctrl. text; If (content = NULL) | content. trim (). length = 0) {EP. seterror (CTRL, "cannot be blank"); Return false;} return true;} // <summary> /// Verify all controls of the form /// </Summary> /// <Param name = "f1"> </param> Public static bool validateall (Form F1) {// If a control fails verification, false bool ISR = true is returned. foreach (control CTRL in f1.controls) {ctrl. focus (); bool tempb = f1.validate (); If (! Tempb) ISR = tempb;} return ISR ;} /// <summary> /// verify the integer /// </Summary> /// <Param name = "Ctrl"> </param> /// <Param name = "EP"> </param> // <returns> </returns> Public static bool validateinteger (control Ctrl, errorprovider EP) {string context = ctrl. text; string pattern = @ "^ ([0-9] {1,}) $"; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (context); If (! ISR) EP. seterror (CTRL, "the input is not a valid integer"); Return ISR ;} /// <summary> /// verify the Positive floating point number /// </Summary> /// <Param name = "? "> </Param> /// <Param name =" EP "> </param> /// <returns> </returns> Public static bool validatepositivefloat (control Ctrl, errorprovider EP) {string context = ctrl. text; string pattern = @ "^ [1-9] D *. D * | 0. D * [1-9] D * $ "; RegEx myregex = new RegEx (pattern); bool ISR = myregex. ismatch (context); If (! ISR) ep. seterror (CTRL, "the input is not a valid integer"); Return ISR ;}}

An example of the verification event in the text box to be verified is as follows:

 
Private void txtoutpath_validating (Object sender, canceleventargs e) {bool bt = winformvalidating. validtepath (txtoutpath, errorprovider1); If (! BT) E. Cancel = true ;}

 

 

 

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.