0
<asp:RegularExpressionValidator ID="revWebsite" runat="server" ForeColor="Red"
 ControlToValidate="txtWebsite" ErrorMessage="Invalid Website (General Details)"
 ValidationExpression="(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?">*
</asp:RegularExpressionValidator>

为什么不工作?:

www.website.com

www.domain.website.com

http://website.com

https://website.com

4

1 回答 1

1

建议:如果你想验证一个站点,你可以通过 pin 来检查它的存在。

您可以为此使用自定义验证器。为此,请参见以下示例:

在 .aspx 页面中:

    <div>
        <asp:TextBox runat="server" ID="txtURL" ValidationGroup="vlg" />
        <asp:RequiredFieldValidator ID="rqfvURL"  ErrorMessage="Please Enter" ControlToValidate="txtURL" ValidationGroup="vlg"
            runat="server" />
        <asp:CustomValidator ID="cstmValURL" ErrorMessage="Please enter valid site" 
            ControlToValidate="txtURL"   runat="server"  ValidationGroup="vlg"
            onservervalidate="cstmValURL_ServerValidate" />
        <asp:Button Text="submit" ID="btn" runat="server" onclick="btn_Click" ValidationGroup="vlg" />
    </ div> 

在 .cs 页面中:

 protected void cstmValURL_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (TestSite())
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        private bool TestSite()
        {         
            Ping objPing = new Ping();
            bool blnResult = false;
            try
            {
                PingReply pngReply = objPing.Send(txtURL.Text.Trim(), 3000);
                if (pngReply.Status == IPStatus.Success)
                   return blnResult= true;
            }
            catch
            {
                return  blnResult=false;
            }
            return blnResult;
        }   

PS这只是一个建议。

于 2013-01-04T12:13:51.633 回答