Generate random code

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. Text. regularexpressions;
Using system. text;

Namespace insapp. Word
{
/// <Summary>
/// String createrandomcode (INT codecount) generates random numbers and letters Based on the length
/// Bool tofilter (string thepara) detects invalid characters. If the parameter is null or contains invalid characters, false is returned. Otherwise, true is returned.
/// Bool checknumber (string getnum) determines whether it is in numeric format
/// Bool checknumberregx (string getnum) determines whether it is a positive or negative number with decimals
/// Bool checknullstr (string getstr) returns true if it is null | false
/// </Summary>
Public class createcode
{

# Region generates random numbers and letters. codecount is the length to be generated.
/// <Summary>
/// Generate random numbers and letters
/// </Summary>
/// <Param name = "codecount"> codecount is the expected length </param>
/// <Returns> </returns>
Public String createrandomcode (INT codecount) // codecount is the length to be generated
{
String allchar = ";
String [] allchararray = allchar. Split (',');
String randomcode = "";
Random Rand = new random ();
Int temp =-1;
For (INT I = 0; I <codecount; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (0, 10 );

Temp = T;
Randomcode + = allchararray [T];
}
Return randomcode;
}
# Endregion

# Region determine whether the digit format is used
/// <Summary>
/// Determine whether the digit format is used
/// </Summary>
/// <Param name = "getnum"> </param>
Public bool checknumber (string getnum)
{

RegEx r = new RegEx (@ "^ [0-9] + $ ");
If (R. ismatch (getnum ))
{
Return true;
}
Else
{
Return false;
}
}
# Endregion

# Region checks invalid characters to prevent SQL Injection

/// <Summary>
/// Check for invalid characters to prevent SQL Injection
/// If the parameter is null, false is returned.
/// If the parameter contains invalid characters, false is returned.
//// Otherwise, true is returned.
/// </Summary>
/// <Param name = "thepara"> </param>
/// <Returns> </returns>
Public bool tofilter (string thepara)
{
String [] badcode = new string [] {"'", "\" "," EXEC "," cmd ","> "," <"," and ", "= ","\\",";"};
Try
{
If (checknullstr (thepara) = false) // If the parameter is null, false is returned.
{
Throw new exception ("the parameter is null ");
}
Else
{
For (INT I = 0; I <badcode. length; I ++)
{
If (thepara. indexof (badcode [I])> 0)
{
Throw new exception ("contains invalid characters ");
}
}
}
Return true;
}
Catch
{
Return false;
}

}
# Endregion

# Region bool checknullstr (string getstr)
/// <Summary>
/// Getstr
/// </Summary>
/// <Param name = "getstr"> value to be checked </param>
/// <Param name = "getshow"> function description of this field: name, sex </param>
Public bool checknullstr (string getstr)
{
Try
{
If (getstr = NULL | getstr = "" | getstr. Length <1)
{
Return false;
}
Else
{
Return true;
}
}
Catch
{
Return false;
}

}
# Endregion

# Region bool checknumberregx (string getnum) Regular Expression to determine whether positive or negative numbers contain Decimals
/// <Summary>
/// Determine whether the digit format is used
/// </Summary>
/// <Param name = "getnum"> </param>
Public bool checknumberregx (string getnum)
{
// ^ [+-]? \ D + (\. \ D + )? $ Plus or minus digits include decimal digits ^ \ D + (\. \ D + )? $
RegEx r = new RegEx (@ "^ \ D + (\. \ D + )? $ ");
If (R. ismatch (getnum ))
{
Return true;
}
Else
{
Return false;
}

}
# Endregion

# Region uses C # to intercept a string of the specified length.
/// <Summary>
// Characters accepted by S
/// L Length
/// </Summary>
/// <Param name = "S"> </param>
/// <Param name = "L"> </param>
/// <Returns> </returns>
Public static string cutstr (string S, int L)
{
String temp = s;
If (RegEx. Replace (temp, "[\ u4e00-\ u9fa5]", "ZZ", regexoptions. ignorecase). Length <= L)
{
Return temp;
}
For (INT I = temp. length; I> = 0; I --)
{
Temp = temp. substring (0, I );
If (RegEx. Replace (temp, "[\ u4e00-\ u9fa5]", "ZZ", regexoptions. ignorecase). Length <= L-3)
{
Return temp + "";
}
}
Return "";
}
# Endregion

# Region random numbers and letters
/// <Summary>
/// Random numbers and letters
/// </Summary>
/// <Param name = "N"> generation length </param>
/// <Returns> </returns>
Public static string rand_number_az_code (int n)
{
Char [] arrchar = new char [] {
'A', 'B', 'D', 'C', 'E', 'E', 'F', 'G', 'h', 'I', 'J ', 'k', 'l', 'M', 'n', 'P', 'R', 'Q', 's', 't', 'U ', 'V', 'w', 'z', 'y', 'x ',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9 ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I', 'J ', 'k', 'l', 'M', 'n', 'Q', 'P', 'R', 't', 's', 'V ', 'U', 'w', 'x', 'y', 'Z'
};

Stringbuilder num = new stringbuilder ();

Random RND = new random (datetime. Now. millisecond );
For (INT I = 0; I <n; I ++)
{
Num. append (arrchar [RND. Next (0, arrchar. Length)]. tostring ());

}

Return num. tostring ();
}
# Endregion

# region letter random number
///


// letter random number
///
/// generation length
//
Public static string randletter (int n)
{< br> char [] arrchar = new char [] {
'A', 'B', 'D', 'C', 'E ', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n', 'P ', 'R', 'Q', 's', 't', 'U', 'V', 'w', 'z', 'y', 'x ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J', 'k', 'l', 'M', 'n', 'Q', 'P', 'R', 't','s ', 'V', 'U', 'w', 'x', 'y', 'z'
};

Stringbuilder num = new stringbuilder ();

Random RND = new random (datetime. Now. millisecond );
For (INT I = 0; I <n; I ++)
{
Num. append (arrchar [RND. Next (0, arrchar. Length)]. tostring ());

}

Return num. tostring ();
}
# Endregion

# Region date Random Function
/// <Summary>
/// Date Random Function
/// </Summary>
/// <Param name = "ra"> length </param>
/// <Returns> </returns>
Public static string daterndname (random RA)
{
Datetime d = datetime. now;
String S = NULL, Y, M, DD, H, mm, SS;
Y = D. year. tostring ();
M = D. Month. tostring ();
If (M. Length <2) M = "0" + m;
Dd = D. Day. tostring ();
If (DD. Length <2) dd = "0" + dd;
H = D. Hour. tostring ();
If (H. Length <2) H = "0" + h;
Mm = D. Minute. tostring ();
If (Mm. Length <2) Mm = "0" + mm;
Ss = D. Second. tostring ();
If (ss. Length <2) Ss = "0" + SS;
S + = Y + M + dd + H + mm + SS;
S + = Ra. Next (100,999). tostring ();
Return S;
}
# Endregion

# Region generation guid
/// <Summary>
/// Generate guid
/// </Summary>
/// <Returns> </returns>
Public static string getguid ()
{
System. guid G = system. guid. newguid ();
Return G. tostring ();
}
# Endregion

}

 

}

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.