Bhavin Rana
Bhavin Rana

Reputation: 1582

website /url validation in asp.net not working

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

why not working for ? :

www.website.com

www.domain.website.com

http://website.com

https://website.com

Upvotes: 0

Views: 1329

Answers (1)

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

Suggestion: if you want to validate a site a you can check its existence by pining it.

you can use custom validator for that. for that see the below example:

in the .aspx page:

    <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> 

in the .cs page:

 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;
        }   

P.S. This is just a suggestion.

Upvotes: 1

Related Questions