日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

常用正则表达式总结(js与C#对照)

發布時間:2024/9/20 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 常用正则表达式总结(js与C#对照) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

js用r.test()而C#用regex.IsMatch()來驗證正則。

大氣象 <%@?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>常用正則表達式總結</title>
????
<script?type="text/javascript"?src="js/jquery-1.3.2.min.js"></script>
????
<script?type="text/javascript">
????????
function?fCheck(){
????????????
//檢查用戶名
????????????var?uname?=?jQuery("#txtUserName").val();
????????????
if(uname?==?""){alert('用戶名不可為空!');return?false;}
????????????r
=/^[\w\d]{4,16}$/;
????????????
if(!r.test(uname)){alert('用戶名為4-16個字符!');return?false;}
????????????
//檢查密碼
????????????var?upwd?=?jQuery("#txtPwd").val();
????????????
if(upwd?==?""){alert('密碼不可為空!');return?false;}
????????????r
=/^.{6,14}$/;
????????????
if(!r.test(upwd)){alert('密碼為6-14個字符!');return?false;}
????????????
if(jQuery("#txtPwdConfirm").val()!=upwd){alert('兩次輸入的密碼不一致!');return?false;}
????????????
//檢查郵箱
????????????var?uemail?=?jQuery("#txtEmail").val();
????????????
if(uemail==""){alert('電子郵件地址不能為空!');return?false;}
????????????r
=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
????????????
if(!r.test(uemail)){alert('電子郵件地址格式不正確!');return?false;}
????????????
????????????
return?true;
????????}
????
</script>
</head>
<body>
????
<form?id="form1"?runat="server">
????
<div>
????用戶名:
????
<asp:TextBox?ID="txtUserName"?runat="server"></asp:TextBox>
????4-16個字符(數字,字母和下劃線)
<br?/>
????
????密碼及確認密碼:
<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?/>
????
????電子郵件地址:
????
<asp:TextBox?ID="txtEmail"?runat="server"></asp:TextBox>(請輸入有效的郵件地址,當密碼遺失時憑此領取)
????
????
<br?/>
????
<asp:Button?OnClientClick=""?ID="btnRegister"?runat="server"?Text="同意以下協議并注冊"?OnClick="btnRegister_Click"?/>
????
</div>
????
</form>
</body>
</html>

?

?

大氣象 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)
????{

????}
????
/****************************************************
?????*?^匹配開始?$匹配結束
?????*?{m,n}匹配從m到n個前面的表達式,比如[\w]{4,16}匹配4到16個字母
?????*?.匹配除?"\n"?之外的任何單個字符。
?????*?要匹配包括?'\n'?在內的任何字符,請使用象?'[.\n]'?的模式。
?????*?[-+.]三個符號,只能有一個。
?????**************************************************
*/
????
protected?void?btnRegister_Click(object?sender,?EventArgs?e)
????{
????????
//用戶名為4-16個字符(數字,字母和下劃線)
????????Regex?regex?=?new?Regex(@"^[\w\d]{4,16}$");
????????
if(!regex.IsMatch(txtUserName.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),?"alert",?"<script>alert('用戶名為4-16個字符(數字,字母和下劃線)。');</script>",?false);
????????????
return;
????????}
????????
//密碼為6-14個字符!
????????regex?=?new?Regex(@"^.{6,14}$");
????????
if?(!regex.IsMatch(txtPwd.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),?"alert",?"<script>alert('密碼為6-14個字符!');</script>",?false);
????????????
return;
????????}
????????
//密碼為6-14個字符!
????????regex?=?new?Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
????????
if?(!regex.IsMatch(txtEmail.Text.Trim()))
????????{
????????????Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),?"alert",?"<script>alert('電子郵件地址格式不正確!');</script>",?false);
????????????
return;
????????}

????????Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),?"alert",?"<script>alert('注冊成功!請登錄。');window.location.href='Login.aspx';</script>",?false);
????}
}

?

?

總結

以上是生活随笔為你收集整理的常用正则表达式总结(js与C#对照)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。