Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everyone,

how to validate website textbox,it takes the
input in the form of http://www.google.com
or just www.googlecom.

Please solve it
Posted

 
Share this answer
 
Match with RegEx ^(http://)?\w+\.\w+\.\w+$ which satisfies your format of URL. You can customize it as per need.
Ref: http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx[^]
 
Share this answer
 
v2
Use regular expression validator
and set expression website url and controltovalidate to textbox id.

use this, it solves your problem.
 
Share this answer
 
you can add a regular expression validator like

C#
<asp:TextBox ID="txtUrl" runat="server"></asp:TextBox>
    
<asp:RegularExpressionValidator 
EnableClientScript="false" 
ID="validatorUrl" 
runat="server"
ValidationExpression="(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
ControlToValidate="txtUrl" 
SetFocusOnError="true" 
ErrorMessage="* Url not valid!" />

code behind :
C#
if (this.validatorUrl.IsValid)
{
  // do stuff
}


this reg-ex is quite syple, there are a lot more complicated

or you can use Uri.TryCreate Method
C#
public static bool TryCreate(Uri baseUri,Uri relativeUri,out Uri result)

Return Value :A Boolean value that is true if the Uri was successfully created; otherwise, false.
 
Share this answer
 
v2
Hi here's the solution-

C# Code To validate site URL using Regular Expression:

C#
using System;
using System.Text.RegularExpressions;

namespace MyApp
{
    public class Class1
    {
        public static void Main(string[] args)
        {
            string Site_URL = "http://IndiHub.com";
            Regex myRegex = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.Compiled);
            bool bResult = myRegex.IsMatch(Site_URL);

            Console.WriteLine(Site_URL  + " Valid URL? " + bResult);

            Site_URL = "TestingWiz.com";

            bResult = myRegex.IsMatch(Site_URL);

            Console.WriteLine(Site_URL + " Valid URL? " + bResult);

            Console.ReadLine();
        }
    }
} 
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Resources;
using System.Net;
using System.Net.Sockets;

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
          if (UrlIsValid(TextBox1.Text.ToString()))
          {
            Label1.Text = "Valid";
          }
            else
          {
            Label1.Text = "InValid";
          }

        }
        public static bool UrlIsValid(string smtpHost)
        {
            bool br = false;
            try
            {
                IPHostEntry ipHost = Dns.Resolve(smtpHost);
                br = true;
            }
            catch (SocketException se)
            {
                br = false;
            }
            return br;
        }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900