JS uses R. Test () and C # uses RegEx. ismatch () to verify the regular expression.
Meteorology
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "regextest. aspx. cs" inherits = "regextest" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Summary of common Regular Expressions </title>
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.3.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
Function fcheck (){
// Check the user name
VaR uname = jquery ("# txtusername"). Val ();
If (uname = "") {alert ('user name cannot be blank! '); Return false ;}
R =/^ [\ W \ D] {4, 16} $ /;
If (! R. Test (uname) {alert ('user name is 4-16 characters! '); Return false ;}
// Check the password
VaR upwd = jquery ("# txtpwd"). Val ();
If (upwd = "") {alert ('password cannot be blank! '); Return false ;}
R =/^. {6, 14} $ /;
If (! R. Test (upwd) {alert ('password is 6-14 characters! '); Return false ;}
If (jquery ("# txtpwdconfirm"). Val ()! = Upwd) {alert ('the passwords entered twice are inconsistent! '); Return false ;}
// Check the email address
VaR uemail = jquery ("# txtemail"). Val ();
If (uemail = "") {alert ('email address cannot be blank! '); Return false ;}
R =/^ \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * $ /;
If (! R. Test (uemail) {alert ('email address format is incorrect! '); Return false ;}
Return true;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
User name:
<Asp: textbox id = "txtusername" runat = "server"> </ASP: textbox>
4-16 characters (numbers, letters, and underscores) <br/>
Password and Confirm Password: <asp: textbox id = "txtpwd" runat = "server" textmode = "password" width = "80px"> </ASP: textbox>
<Asp: textbox id = "txtpwdconfirm" runat = "server" textmode = "password" width = "80px"> </ASP: textbox>
<Br/>
Email Address:
<Asp: textbox id = "txtemail" runat = "server"> </ASP: textbox> (enter a valid email address, which is used when the password is lost)
<Br/>
<Asp: button onclientclick = "" id = "btnregister" runat = "server" text = "agree to the following agreement and register" onclick = "btnregister_click"/>
</Div>
</Form>
</Body>
</Html>
Meteorology
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Text. regularexpressions;
Public partial class regextest: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
/*************************************** *************
* ^ Match start $ match end
* {M, n} matches the expression from m to N. For example, [\ W] {4, 16} matches 4 to 16 letters.
*. Match any single character except "\ n.
* To match any character including '\ n', use the mode like' [. \ n.
* [-+.] Three symbols can only be one.
**************************************** ***********/
Protected void btnregister_click (Object sender, eventargs E)
{
// The username must be 4-16 characters long (numbers, letters, and underscores)
RegEx = new RegEx (@ "^ [\ W \ D] {4, 16} $ ");
If (! RegEx. ismatch (txtusername. Text. Trim ()))
{
Page. clientscript. registerclientscriptblock (this. GetType (), "alert", "<SCRIPT> alert ('user name is 4-16 characters (numbers, letters, and underscores ). '); </SCRIPT> ", false );
Return;
}
// The password is 6-14 characters long!
RegEx = new RegEx (@ "^. {6, 14} $ ");
If (! RegEx. ismatch (txtpwd. Text. Trim ()))
{
Page. clientscript. registerclientscriptblock (this. GetType (), "alert", "<SCRIPT> alert ('password is 6-14 characters! '); </SCRIPT> ", false );
Return;
}
// The password is 6-14 characters long!
RegEx = new RegEx (@ "^ \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * $ ");
If (! RegEx. ismatch (txtemail. Text. Trim ()))
{
Page. clientscript. registerclientscriptblock (this. GetType (), "alert", "<SCRIPT> alert ('email address format is incorrect! '); </SCRIPT> ", false );
Return;
}
Page. clientscript. registerclientscriptblock (this. GetType (), "alert", "<SCRIPT> alert ('registration successful! Please log on. '); Window. Location. href = 'login. aspx'; </SCRIPT> ", false );
}
}