Sample code for C # common regular validation functions

Source: Internet
Author: User
This article mainly introduces C # common regular validation http://www.php.cn/wiki/145.html "target=" _blank "> function, an example of C # for IP authentication, price verification and positive integer verification of the relevant operational skills, A friend you need can refer to the following

The examples in this paper describe C # common regular validation functions. Share to everyone for your reference, as follows:

1. IP Address Verification

<summary>///IP Address authentication///</summary>public static bool Checkip (string IP) {  bool result = false;  Regex ipreg = new Regex (@ "^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. ( \D{1,2}|1\D\D|2[0-4]\D|25[0-5]) \.  (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $ ");  if (Ipreg.ismatch (IP))  {    result = true;  }  return result;}

2. Price verification

<summary>///Price Verification///</summary>///<param name= "Pricestr" ></param>///<returns> </returns>public bool Checkprice (string pricestr) {  bool result = false;  Regex regex = new Regex (@ "^\d+ (\.\d{1,2})? $", regexoptions.ignorecase);  Match match = regex. Match (PRICESTR);  if (match. Success)  {    result = true;  }  return result;}

3, positive integer verification

<summary>///positive integer validation//</summary>public static bool Checkpositiveinteger (string numstr) {  bool result = false;  Regex regex = new Regex (@ "^[1-9]\d*$", regexoptions.ignorecase);  Match match = regex. Match (NUMSTR);  if (match. Success)  {    result = true;  }  return result;}

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.