ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work

本文探讨了Visual Studio提供的URL验证正则表达式的局限性,特别是当URL中包含特殊字符“+”时会出现的问题。文章提供了两种改进的正则表达式方案来解决这一问题,确保URL验证更加准确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work

"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" this regular expression is given by Visuial studio. we use that to valid Url format.

But if the the url contains "+" symbol. we will see the an error message.

 

We can change the regular expression to "http(s)?://([\w-]+\.)+[\w-]+(/[\w-+ ./?%&=]*)?"

or "(http|https):(?://(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(?::(\d*))?(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?|(/?(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?)(?:\?((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %&gt; <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>用户注册</title> <style type="text/css"> .error { color: red; } </style> </head> <body> <form id="form1" runat="server"> <div> <label for="txtName">姓名:</label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="用户必须填写" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator> </div> <div> <label for="txtPassword">密码:</label> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPassword" ValidationExpression="^.{8,}$" ErrorMessage="密码必须八位以上" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label for="txtConfirmPassword">确认密码:</label> <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtPassword" ErrorMessage="两次密码必须一致" ForeColor="Red" Display="Dynamic"></asp:CompareValidator> </div> <div> <label for="txtPhone">手机号:</label> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPhone" ValidationExpression="^1[35789][0-35-9]\d{8}$" ErrorMessage="手机号格式不正确" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label>请修正以下错误:</label> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="true" ShowMessageBox="false" HeaderText="" CssClass="error"></asp:ValidationSummary> </div> <div> <asp:Button ID="btnRegister" runat="server" Text="注册" OnClick="btnRegister_Click" /> </div> </form> </body> </html>
最新发布
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值